公司要实现一个界面,非常类似手机京东的界面,下拉可以刷新,上拉可以加载更多。前同事用了XRecyclerview实现下拉刷新,上拉加载更多。然而,XRecyclerview的Header(顶部不规则的部分)用了一个ListView来实现垂直走马灯的效果,这就和XRecyclerview造成了冲突,导致XRecyclerview下拉卡顿。摸索了很久,试过重写ListView的onMeasure方法,也重写过RecyclerView的onMeasure方法,也试过监听RecyclerView的addOnItemTouchListener,都无济于事。最后,设置ListView的focusable为false解决了。注意:在xml中设置是无效的!!!,要在代码中设置:
lv_scroll.setFocusable(false);如果同时设置
lv_scroll.setEnabled(false);参考这里 那么ListView将不能响应手指滑动,更加完美。 最后,除了解决这个冲突,公司还要求实现监听RecyclerView滑动,手指向上推的时候,顶部title逐渐出现,向下划则逐渐隐藏title。 实现如下: Fragment的xml,根布局是FrameLayout,那么title显示也不会影响RecyclerView。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/yellow_bg"> <com.sf.sdk.xrecyclerview.XRecyclerView android:id="@+id/rv_order" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"/> <TextView android:id="@+id/tv_empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center|bottom" android:text="暂无数据" android:textColor="@color/gray_text_title" android:textSize="@dimen/txtsize_body" android:drawableTop="@mipmap/order_normal" android:layout_marginBottom="@dimen/margin_small" android:drawablePadding="5dp"/> <RelativeLayout android:id="@+id/rl_titlebar" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/colorPrimary" android:orientation="horizontal" android:visibility="gone"> <TextView android:id="@+id/titlebar_tv" style="@style/TextViewTitle" android:layout_centerInParent="true" android:gravity="center" android:textColor="@color/black_text" android:text="我是标题" android:textSize="18sp" /> <View android:id="@+id/view_split" android:layout_width="match_parent" android:layout_alignParentBottom="true" android:layout_height="@dimen/height_divider" android:background="@color/gray_line" /> </RelativeLayout> </FrameLayout>header的xml就不贴了,主要是一些复杂不规则的界面,包含一个ListView。 监听RecyclerView:
rv_order.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (dy > 0) { // 手指网上推,显示title showTitle(); } else { // 手指往下推,隐藏title hideTitle(); } } });两个动画效果:
/** * 隐藏title */ private void hideTitle() { if (rl_title.getVisibility() == View.VISIBLE) { TranslateAnimation moveAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.hide_to_top); rl_title.setVisibility(View.GONE); rl_title.startAnimation(moveAnimation); moveAnimation.setFillAfter(true); } } /** * 显示Title */ private void showTitle() { if (rl_title.getVisibility() == View.GONE) { TranslateAnimation showAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.show_from_top); rl_title.setVisibility(View.VISIBLE); rl_title.startAnimation(showAnimation); showAnimation.setFillAfter(true); } }动画文件show_from_top.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="800" android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="-100%" android:toYDelta="0" />动画文件hide_to_top.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="800" android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="-100%" />