使视图沿RecyclerView滚动

时间:2016-12-28 20:05:50

标签: android android-recyclerview scrollview android-scrollview

好吧,我正试图制作一款顶级酒吧,就像Youtube应用程序中的顶级酒吧一样。它几乎适用于所有情况,但是当我使用RecyclerView的ScrollListener时,我遇到了问题。

正如您所见,视图在滚动时的某个时刻不会改变其位置。

这是我的代码:

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);
            }

        }


    });

2 个答案:

答案 0 :(得分:1)

您应该使用TabLayout

尝试ViewPager
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="stellar.kade_c.com.MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

答案 1 :(得分:0)

您应该将TabLayout与ViewPager一起用于此