Android 使用 RecyclerView 的 SwipeRefreshLayout

Android SwipeRefreshLayout with RecyclerView

这是我的代码设置:

目前我有一个 Main Activity,然后创建一个带有 getSupportFragmentManager() 的片段。片段包含 RecyclerView(用于卡片视图),如下面 xml 中所定义:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" />

</RelativeLayout>

这是我主要的布局 activity:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        ...

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/activity_main_swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

主 activity 实例化片段,然后创建自己的 RecyclerView, LinearLayoutManager, and Adapter 与主 activity 没有任何连接。

主要 activity 创建 SwipeRefreshLayout,如上文 xml 所示。

我遇到的问题是,当我在 activity 中向上滚动时,一旦列表无法进一步向上滚动,如果我继续向下滑动并刷新,刷新指示器就会下拉。 我不希望指示器下拉,直到我释放当前触摸并再次下拉。

由于我无法从我的 activity 直接访问回收器视图以添加 OnScrollListener 并以编程方式启用和禁用 SwipeRefreshLayout,我应该如何解决这个问题?

感谢所有回答!

Since I cannot directly access the recycler view from my activity to add an OnScrollListener and enable and disable the SwipeRefreshLayout programmatically, how should I go about solving this?

是的,你可以!

首先实现RecyclerView.OnScrollListener so that your fragment can listen to scroll events. To do so you have to subclass RecyclerView.OnScrollListener: https://gist.github.com/ArtworkAD/ae6241d60282a54adfa22d28cddb48ae

下一步是将滚动事件委托给托管 activity 以更改滑动布局。您的片段可能有以下添加:

public class SomeFragment extends Fragment implements OnScrollStateListener {

    public interface ScrollEventListener {
        void onScroll(int dx, int dy)
        void onScrollStateChanged(int state)
    }

    ScrollEventListener scrollEventListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Give the recycler view a scroll listener
        recyclerView.addOnScrollListener(new DefaultRecycleViewScrollListener(this));
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        // delegate the event to the listener
        scrollEventListener.onScrollStateChanged(newState);
    }

    @Override
    public void onScrolled(int dx, int dy) {
        // delegate the event to the listener
        scrollEventListener.onScrolled(dx, dy);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // activity should implement the interface
        if (context instanceof ScrollEventListener) {
          scrollEventListener = (ScrollEventListener) context;
        }
    }
}

确保在 activity 中实施 ScrollEventListener 以接收滚动事件。

public class SomeActivity extends Activity implements SomeFragment.ScrollEventListener {

     @Override
     public void onScroll(int dx, int dy) {
         // access swipe layout here
     }

     @Override
     public void void onScrollStateChanged(int state){
         if (state == SCROLL_STATE_DRAGGING) {
             // disable swipe
         } else {
             // enable
         }
     }
}