【问题标题】:Make View scrolls along RecyclerView使视图沿 RecyclerView 滚动
【发布时间】:2016-12-28 20:05:50
【问题描述】:

嗯,我正在尝试制作一个类似于 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);
            }

        }


    });

【问题讨论】:

  • 你应该更喜欢 TabLayout 和 viewpager 设置可见分隔线高度。

标签: android android-recyclerview scrollview android-scrollview


【解决方案1】:

你应该试试TabLayoutViewPager

<?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>

【讨论】:

    【解决方案2】:

    您应该为此使用 TabLayout 和 ViewPager

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 2016-01-12
      相关资源
      最近更新 更多