I am connecting two recyclerView as you seen in the picture.
one of them is sectioned recyclerView with header. (the bottom one)
another one is a recyclerView that shows the headers of bottom one as tab. (the top one)
I dynamically get the dataset for them with Retrofit
and setting them separately
I want to highlight the top one items by scrolling the bottom one. Scrolling is successfully done and when bottom recyclerView scrolled and get to its header position, top recyclerView goes to that specific position. Now as I scrolled to that position, also I want to highlight that.
I use an xml file for change the color of item when selected :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/chooseCAndZ" android:state_selected="true"/>
<item android:drawable="@color/colorAccent" android:state_focused="true"/>
</selector>
and do the below job for change the color of top recyclerView when clicked and it works great :
@Override
public void onBindViewHolder(final FoodTopMenuAdapter.ViewHolder holder, int position) {
holder.mView.setSelected(selectedPos == holder.getAdapterPosition());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer index = inList.get(holder.getAdapterPosition());
linearLayoutManager.scrollToPosition(index);
if (holder.getAdapterPosition() == RecyclerView.NO_POSITION) return;
notifyItemChanged(selectedPos );
selectedPos = holder.getAdapterPosition();
notifyItemChanged(selectedPos );
}
});
}
also do this job for scrolling to the specific position in bottom recyclerView onBindViewHolder
:
index = inList.get(vheader.getHeaderCategory());
foodRVLayoutManager.scrollToPosition(index);
now the idea is to get the specific view from foodRVLayoutManager.getChildAt(i).setSelected(true);
to change the view color as selected
.
but the main problems are these :
** the
foodRVLayoutManager
just can get children that are visible! it means that changing the color is works for visible children.** the
onClick
andscrollToPosition
interference each other.
finally I solved the problem just by changing
foodRVLayoutManager.scrollToPosition(index);
tofoodRVLayoutManager.scrollToPositionWithOffset(index, 3);
by setting offset, invisible items become visible before highlight.