下面是我页面布局的一个手绘图,主要实现思想是:RecyclerView用于展示列表数据,button是一个退出登录按钮(未登录状态下是隐藏的);
预想的实现效果:button隐藏状态下,正常的滑动;button显示状态下,button在recyclerView的下面,作为一个整体进行滑动;
但是由于Recycler View与Scroll View嵌套在一起使用时会出现滑动冲突,
我遇到的滑动冲突效果是:在Button显示状态下,由于Recycler View和Scroll View都存在滑动,当滑动到底部将button显示在屏幕底部后,再向上滑动时,滑动操作会先在Recycler View中执行,导致button在屏幕底部不动,Recycler View列表开始滑动,滑动到一定距离后,当Recycler View的滑动结束后,才开始执行Scroll View的滑动,button才开始慢慢消失,很不好的用户体验,得想办法干掉他!
初步思路是:把Recycler View的滑动禁用掉,所有的滑动都由Scroll View来实现;
尝试一:通过给recycler View对象设置滑动监听:
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
通过在onTouch()方法中设置false去实现,结果不尽人意;后来了解了该方法后,才明白,这个方法的方法体是对屏幕上的滑动操作进行处理,返回值为true时,会将这个滑动操作传递给上级(即Scroll View)继续执行滑动操作;返回值为false时,是指将屏幕传过来的滑动操作在recycler View中处理掉,不在传递给Scroll View;——所以对本问题无任何用处;
尝试二:通过给RecyclerView对象设置布局:
recyclerView.setLayoutManager(new LinearLayoutManager(context){
@Override
public boolean canScrollVertically() {
return false;
}
});
字面意思,就是将recycler view的垂直滑动禁用掉(想要禁用掉recycler view的水平滑动只需重写canScrollHorizontally()方法,返回值设为false即可);运行效果确实将recycler view的垂直滑动禁用掉了,但是recycler view的高度却只有屏幕高度那么高,可想而至,recyclerview的内容没有显示完整,但至少实现了一部分预想的效果了;
接下来就是想法把recycler view的内容显示完整了。
找到一个比较简单实用的方法,就是在Recycler View的外层加一层RelativeLayout即可,最后我的xml布局如下:
<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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<Button
android:id="@+id/loginout_btn"
android:layout_width="match_parent"
android:layout_height="@dimen/list_column_main_title_height"
android:background="@drawable/btn_frame_style"
android:text="退出账号"
android:textColor="@color/btn_login_out_text_color"
android:textSize="18dp" />
</LinearLayout>
</ScrollView>
最终以尝试二中的方式解决了本问题,实现了预想的滑动效果~~
通过本问题的解决,对Android开发中的布局滑动机制有了很大的提高,特此记录一下。
***写下回忆,记录心情***