
點(diǎn)擊左邊的type標(biāo)簽可以切換右邊對應(yīng)的列表數(shù)據(jù)的位置,滑動右邊的列表可以自動匹配到左邊的type標(biāo)簽。具體實(shí)現(xiàn)可以用兩個Recyleview來實(shí)現(xiàn),利用smoothScrolltoPosition()方法。
/**
* Starts a smooth scroll to an adapter position.
* <p>
* To support smooth scrolling, you must override
* {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} and create a
* {@link SmoothScroller}.
* <p>
* {@link LayoutManager} is responsible for creating the actual scroll action. If you want to
* provide a custom smooth scroll logic, override
* {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} in your
* LayoutManager.
*
* @param position The adapter position to scroll to
* @see LayoutManager#smoothScrollToPosition(RecyclerView, State, int)
*/
public void smoothScrollToPosition(int position) {
if (mLayoutFrozen) {
return;
}
if (mLayout == null) {
Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. "
+ "Call setLayoutManager with a non-null argument.");
return;
}
mLayout.smoothScrollToPosition(this, mState, position);
}
由于recycleview緩存的機(jī)制,這個方法有時(shí)達(dá)不到想要的效果所以要再進(jìn)行判斷結(jié)合smoothScrollBy方法來實(shí)現(xiàn)效果
int first = mGridLayoutManager.findFirstVisibleItemPosition();
int last = mGridLayoutManager.findLastVisibleItemPosition();
int pos = mItemCountList2.get(position);
if (pos < first) {
LogUtils.LogE("0");
mRvRight.smoothScrollToPosition(pos);
1.當(dāng)你想要切換的位置小于右邊列表第一個可見的條目位置,這個方法可以達(dá)到的效果
} else if (pos >= first && pos < last) {
int top = mRvRight.getChildAt(pos - first).getTop();
LogUtils.LogE("top = " + top);
mRvRight.smoothScrollBy(0, top);
2.當(dāng)你想要切換的位置大于等于右邊第一個可見條目的位置并且小于最后一個可見條目的時(shí)候利用smoothScrollBy方法實(shí)現(xiàn)
else if (pos >= last) {
LogUtils.LogE("2");
mRvRight.smoothScrollToPosition(pos);
3.當(dāng)切換的位置大于等于右邊最后一個可見條目smoothScrollToPosition方法只能把該條目置底不能置頂,所以在滾動結(jié)束后再利用
smoothScrollBy方法強(qiáng)制移動到頂部
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
int last = mGridLayoutManager.findLastVisibleItemPosition();
if (mDatasBeanList.get(last).getLabel() != null && mDatasBeanList.get(last).getLabel().equals(mTag)) {
mRvRight.smoothScrollBy(0, mHeight - mItemHeight);
}
}
代碼已上傳https://github.com/digtal/recycleview-study
有問題留言溝通哈!