Desired Result
Maintain scroll position and give right position on specific view of custom row.
I am using RecyclerView in which I am trying to add on Demand Data on beginning of list as well as end of list.
Fetching data through Scrolling bottom is working perfectly.
But Scrolling top having issues as follows:
- Set Click listener in Adapter's onBindViewHolder.
- Set Click listener in Recyclerview's OnItemTouchListener.
Here is the case:
When I scroll up the list and then It loads 15 data and add it to the top of list. Our intention is to Maintain scroll position. Which can be achieved through
adapter.notifyItemRangeInserted(0, stringArrayListTemp.size());
But the Issue is while I am clicking on any Item without scrolling then View's OnClickListener gives wrong position. You can check adapter's method:
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
String item = moviesList.get(position);
holder.title.setText(item);
holder.title.setTag(position);
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("onClick", "stringArrayList position: " + moviesList.get(position) + " - view position: " + moviesList.get((int) view.getTag()));
}
});
}
But at the same time in Recyclerview's addOnItemTouchListener gives right position with stringArrayList.get(position)
but gives wrong position using getTag.
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.e("onItemClick", "stringArrayList position: "+stringArrayList.get(position)+ " - view position: "+stringArrayList.get((int)view.getTag()));
}
}));
When I try to add data in the beginning of the list by following I am getting the exact issue.
// retain scroll position and gives wrong position onClick
adapter.notifyItemRangeInserted(0, stringArrayListTemp.size());
// does not retain scroll position, scroll to top & gives right position onClick
//adapter.notifyDataSetChanged();