简体   繁体   English

Android SwipeRefreshLayout与RecyclerView

[英]Android SwipeRefreshLayout with RecyclerView

Here is my code setup: 这是我的代码设置:

Currently I have a Main Activity which then creates a spawns a fragment with getSupportFragmentManager() . 目前,我有一个Main Activity,然后使用getSupportFragmentManager()创建一个片段。 The Fragment contains a RecyclerView (for a card view) as defined in the xml below: 片段包含一个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>

And this is the layout of my main 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>

The main activity instantiates the fragment which then creates its own RecyclerView, LinearLayoutManager, and Adapter without any connections to the main activity. 主要活动实例化片段,然后片段创建其自己的RecyclerView, LinearLayoutManager, and Adapter而与主要活动没有任何连接。

The main activity creates the SwipeRefreshLayout as seen in the xml above. 主要活动将创建SwipeRefreshLayout,如上面的xml所示。

The problem I have is the when I scroll up in my activity, once the list cannot scroll any further up the refresh indicator pulls down if I keep swiping down and refreshes. 我遇到的问题是,当我向上滚动活动时,如果列表无法进一步向上滚动,则如果我继续向下滑动并刷新,则刷新指示器将下降。 I don't want the indicator to pull down until I release the current touch and pull down again. 在释放当前触摸并再次下拉之前,我不希望指示器下拉。

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? 由于无法直接从活动中访问回收者视图来添加OnScrollListener并以编程方式启用和禁用SwipeRefreshLayout,因此该如何解决呢?

All answers appreciated! 所有答案表示赞赏!

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? 由于无法直接从活动中访问回收者视图来添加OnScrollListener并以编程方式启用和禁用SwipeRefreshLayout,因此该如何解决呢?

Yes, you can! 是的你可以!

First implement RecyclerView.OnScrollListener so that your fragment can listen to scroll events. 首先实现RecyclerView.OnScrollListener,以便您的片段可以侦听滚动事件。 To do so you have to subclass RecyclerView.OnScrollListener : https://gist.github.com/ArtworkAD/ae6241d60282a54adfa22d28cddb48ae 为此,您必须将RecyclerView.OnScrollListener子类化: https : //gist.github.com/ArtworkAD/ae6241d60282a54adfa22d28cddb48ae

The next step is to delegate the scroll events to the hosting activity to make changes to the swipe layout. 下一步是将滚动事件委托给托管活动,以更改滑动布局。 Your fragment may have following additions: 您的片段可能添加了以下内容:

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

Make sure to implement ScrollEventListener in your activity to receive scroll events. 确保在您的活动中实现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
         }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM