最近项目中用到了RecyclerView,在此记录一下(界面中也存在其他交互控件),例如下面这张图:图片上方是五个TextView,垂直排列,下方是一个用于展示的RecyclerView,为了解决ScrollView与RecyclerView的滑动冲突,将RecyclerView的滑动交给Scrollview来完成,需要如下代码: 首先是.xml文件中:
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2019" android:textColor="#529BFD" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:textSize="16sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2018" android:textColor="#4A7BFF" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="16sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2017" android:layout_marginTop="10dp" android:layout_marginLeft="15dp" android:textColor="#529BFD" android:textSize="16sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2016" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:textColor="#4A7BFF" android:textSize="16sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2015" android:layout_marginTop="10dp" android:layout_marginLeft="25dp" android:textColor="#529BFD" android:textSize="16sp"/> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> </android.support.v4.widget.NestedScrollView>ScrollView替换成android.support.v4.widget.NestedScrollView。 在Java中,适配器设置,添加如下代码:
recyclerview.setNestedScrollingEnabled(false);如此,便可完美解决滑动冲突问题。
有错误之处烦请各位大佬指正,谢谢!