0
好吧,我正在試着製作一個像Youtube應用程序那樣的頂部欄。它幾乎適用於所有情況,但是當我使用RecyclerView中的ScrollListener時,出現問題。沿着RecyclerView製作視圖卷軸
正如你可以看到,當滾動查看並不在某一時刻改變其位置。
這裏是我的代碼:
rvTop.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) layoutManagerTop;
if(filterTopAdapter.lastSelected >= layoutManager.findFirstVisibleItemPosition() && filterTopAdapter.lastSelected <= layoutManager.findLastVisibleItemPosition()) {
selectionLine.setVisibility(View.VISIBLE);
final View view = rvTop.findViewHolderForAdapterPosition(filterTopAdapter.lastSelected).itemView;
selectionLine.setTranslationX(view.getLeft() - rvTop.getScrollX());
Log.d("Scroll", String.valueOf(view.getLeft() - rvTop.getScrollX()));
}
else {
selectionLine.setVisibility(View.GONE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager layoutManager = (LinearLayoutManager) layoutManagerTop;
if(filterTopAdapter.lastSelected >= layoutManager.findFirstVisibleItemPosition() && filterTopAdapter.lastSelected <= layoutManager.findLastVisibleItemPosition()) {
selectionLine.setVisibility(View.VISIBLE);
final View view = rvTop.findViewHolderForAdapterPosition(filterTopAdapter.lastSelected).itemView;
selectionLine.setTranslationX(view.getLeft() - rvTop.getScrollX());
Log.d("Scroll", String.valueOf(view.getLeft() - rvTop.getScrollX()));
}
else {
selectionLine.setVisibility(View.GONE);
}
}
});
你應該更喜歡TabLayout與viewpager設置可見分隔高度。 –