SwipeRefreshLayout 向上滚动时阻碍recycleview的滚动

问题描述 投票:0回答:4

在我的回收视图中,当我在列表中向上滚动时,swiperefreshlayout 被触发,这阻碍了我的列表向上移动。我浏览了 this 博客,但它解决了列表视图的这个问题。对于 recycleview 的这个问题是否有任何类似的解决方法,因为本博客中使用的 onscrollListener 已被弃用。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    tools:context="com.morpho.innovationlab.trustwall.Dashboard">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

                <ImageView
                    android:id="@+id/backdrop"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="10dp"
                    android:importantForAccessibility="no"
                    android:src="@drawable/tw_logo"
                    app:layout_collapseMode="parallax"
                    app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="40dp"
                    android:layout_weight="1"
                    android:orientation="vertical">


                    <TextView
                        android:id="@android:id/text1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Trustwall"
                        android:textColor="@android:color/white"
                        android:textSize="24sp"
                        app:layout_collapseMode="parallax"
                        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Your Privacy is our priority"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:layout_collapseMode="parallax"
                        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />
                </LinearLayout>

            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView

                android:id="@+id/emptyview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="50dp"
                android:gravity="center_horizontal"
                android:text="Click on the Add button to add websites to trustwall secure zone."
                android:visibility="gone" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_website"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_add"
        app:elevation="12dp"
        app:pressedTranslationZ="12dp" />
    />
</android.support.design.widget.CoordinatorLayout>
android listview android-recyclerview swiperefreshlayout
4个回答
4
投票

我终于通过实现 recycleview 的 setOnScrollListenner 实现了正确的行为。

 @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            int topRowVerticalPosition =
                    (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
            swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);

        }

0
投票

都是一样的idea

您可以自定义 RecycleView 并设置何时消耗触摸事件或绕过。

然后 SwipeRefreshLayout 可以接收触摸事件。


0
投票

试试这个

我无法实施@Manish Patiyal 已回答的 setOnScrollListenner,因此我尝试了另一种适合我的解决方案。

 recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() { //Used to restrict collapsing of recycler view horizontal swipe and swipe refresh layout
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (linearlayoutmanager.findFirstCompletelyVisibleItemPosition() == 0)
                swipeRefreshLayout.setEnabled(true);
            else
                swipeRefreshLayout.setEnabled(false);
        }
    });

0
投票
**This Swipe Refresh Layout Util function to use any place to call on application and perform your requirement in listener  **



    public static void setMoveUpOnRefreshListener( final SwipeRefreshLayout layout, final OnRefreshListener listener,RecyclerView recyclerView) {
        if (layout != null) {
            layout.setOnChildScrollUpCallback((parent, child) -> {
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                layout.setEnabled(topRowVerticalPosition >= 0);
                return false;
            });
            layout.setOnRefreshListener(() -> {
//                Logger.log("mTopSwipeRefreshLayout onRefresh");
              
                layout.setRefreshing(true);
                if (listener != null) {
                    listener.onRefresh();
                }

            });
            // sets the colors used in the refresh animation
            layout.setColorSchemeResources(
                    R.color.primarydark,
                    R.color.primary);
        }
    }



public interface OnRefreshListener {
    void onRefresh();
}
© www.soinside.com 2019 - 2024. All rights reserved.