天天看點

解決SpringView與CoordinatorLayout和AppBarLayout嵌套使用的上下滑動沖突問題

最近再做一個項目的時候,同時使用了SpringView以及CoordinatorLayout嵌套AppBarLayout的布局,在測試的時候,發現SpringView的是滑動事件與CoordinatorLayout中的滑動事件沖突。經過測試,發現可以用AppBarLayout的狀态來解決此問題。

xml布局為:

<com.gsitv.view.PullRefresh.SpringView
        android:id="@+id/pullrefreshlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:orientation="vertical">
            <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="#00000000"
                   app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    app:statusBarScrim="@color/white">                     
                </android.support.design.widget.CollapsingToolbarLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/function_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:background="@color/white" />

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

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recommend_recycler"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                    </android.support.v7.widget.RecyclerView>                      

            </android.support.v4.widget.NestedScrollView>
        </android.support.design.widget.CoordinatorLayout>

    </com.gsitv.view.PullRefresh.SpringView>
           

Java代碼為:

app_bar = (AppBarLayout) contentView.findViewById(R.id.appbar);
    springView = (SpringView) contentView.findViewById(R.id.pullrefreshlayout);
    springView.setType(SpringView.Type.FOLLOW);
    springView.setListener(new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {            
                    springView.onFinishFreshAndLoad();
                }
            }, 1000);
        }

        @Override
        public void onLoadmore() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {                   
                    springView.onFinishFreshAndLoad();
                }
            }, 1000);
        }
    });
     springView.setHeader(new DefaultHeader(getActivity()));
    app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener(){
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if(verticalOffset==0){
                springView.setEnable(true);
            }else {
                springView.setEnable(false);
            }
        }
    });
           

解決此問題的關鍵代碼便是。根據AppBarLayout的狀态來判斷SpringView的事件是true還是false,當AppBarLayout處于展開狀态時,SpringView的狀态設定為true,當AppBarLayout狀态為折疊或者正處于展開折疊的過程中時,将SpringView的狀态設定為false。即以下代碼:

app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener(){
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if(verticalOffset==0){
                springView.setEnable(true);
            }else {
                springView.setEnable(false);
            }
        }
    });