As the name suggests RecyclerView, the views in it are recycled. When you are using a recyclerView and use some small views, for example- say textviews and images etc., in its row then on scrolling the recyclerview the views in the row change. This happens because the views get recycled and reused as onBindViewHolder method in its adapter is called several times as recyclerview needs a view unless it has a new one. So each time you set visibility in child views, other views states are also changed. So whenever you scroll up and down, these views are getting re-drawn with wrong visibility options.
Solution to this problem is very simple.
First take a boolean value to save the clicked value of the item or view as follows:
holder.imgVw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isClicked) {
holder.txtvw.setVisibility(View.VISIBLE);
} else {
holder.txtvw.setVisibility(View.GONE);
}
}
});
Then simply register an Interface in your adapter on the click of particular child view as follows:
holder.imgVw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yourListener.onSelectingView(position, holder.txtvw.getId);
if (isClicked) {
holder.txtvw.setVisibility(View.VISIBLE);
} else {
holder.txtvw.setVisibility(View.GONE);
}
}
});
Now in your activity, in the implemented methods of your listener do the following:
@Override
public void onSelectingView(int position, String id) {
adapter.notifyDataSetChanged();
mRecyclerView.invalidate();
}
That's all.. your view will not change its visibility on scrolling of the recyclerview.
Happy coding and remember "Code to make it better..!!"
CapCut VPN APK is a reliable app that helps users secure their internet connection while using CapCut or other apps on Android devices. It offers fast, stable, and private VPN connections for safer browsing and app usage. The how to CapCut VPN app pairing guide helps users connect their devices to the VPN quickly and easily. The app runs smoothly on most Android devices and provides an intuitive, user-friendly interface.
ReplyDelete