Android的滑動(dòng)沖突可以說(shuō)是老生常談了舍沙,在項(xiàng)目中會(huì)遇到各種各樣的滑動(dòng)沖突問(wèn)題讼载,本遍文章主要對(duì)在ScrollView中多個(gè)RecyclerView的滑動(dòng)沖突的處理,當(dāng)然會(huì)有一部分使用第三部分上下拉刷新加載(SmartRefreshLayout,TwinklingRefreshLayout)的沖突的處理方式像街。(本次例子使用的是SmartRefreshLayout)
當(dāng)ScrollIView內(nèi)部只有一個(gè)RecyclerView的時(shí)候
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--其它的View-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="這個(gè)是RecyclerView"
android:textColor="#000"
android:textSize="24sp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</ScrollView>
OneRecyclerView.gif
當(dāng)ScrollIView內(nèi)部有多個(gè)RecyclerView的時(shí)候
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--其它的View-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="這個(gè)是RecyclerView"
android:textColor="#000"
android:textSize="24sp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
當(dāng)ScrollView嵌套多個(gè)RecyclerView時(shí)設(shè)置RecyclerView的android:nestedScrollingEnabled="false"筹裕,將滑動(dòng)事件交給父類(lèi)的ScrollView去處理,并且每個(gè)RecyclerView外面包上一層RelativeLayout椎木,設(shè)置如下屬性违柏,效果展示出下:
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
MultiRecyclerView.gif
當(dāng)ScrollIView內(nèi)部只有一個(gè)RecyclerView的時(shí)候并且外部嵌套上下拉刷新控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableHeaderTranslationContent="false"
app:srlEnableLoadmore="true"
>
<com.scwang.smartrefresh.header.MaterialHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--其它的View-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="這個(gè)是RecyclerView"
android:textColor="#000"
android:textSize="24sp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</RelativeLayout>
OneRecyclerViewWithRefresh.gif
當(dāng)ScrollIView內(nèi)部有多個(gè)RecyclerView的時(shí)候并且外部嵌套上下拉刷新控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableHeaderTranslationContent="false"
app:srlEnableLoadmore="true"
>
<com.scwang.smartrefresh.header.MaterialHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--其它的View-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="這個(gè)是RecyclerView"
android:textColor="#000"
android:textSize="24sp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</RelativeLayout>
MultiRecyclerViewWithRefresh.gif
以上為在ScrollView中嵌套R(shí)ecyclerView產(chǎn)生的滑動(dòng)沖突問(wèn)題的解決方案,如有不當(dāng)?shù)牡胤较阕担瑲g迎指正漱竖,謝謝
最后附上GitHub地址RecyclerViewInScrollView