ScrollView巢狀RecyclerView出現的滑動以及顯示異常的處理
阿新 • • 發佈:2019-01-09
解決問題:1.滑動衝突 2.顯示不完整
1.滑動衝突:
1.1.重寫ScrollView
package com.cexi.yunzehui.view; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; /** * Created by Hsh on 2017/4/6. * 解決內嵌 * * 橫向滑動或者豎向的view 滑動不靈敏的問題 * 外層攔截法 *核心:不攔截橫向滑動,攔截豎向 */ public class NestedScrollView extends ScrollView { private float mDownPosY; private float mDownPosX; public NestedScrollView(Context context) { super(context); } public NestedScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final float x = ev.getX(); final float y = ev.getY(); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mDownPosX = x; mDownPosY = y; break; case MotionEvent.ACTION_MOVE: final float deltaX = Math.abs(x - mDownPosX); final float deltaY = Math.abs(y - mDownPosY); if (deltaX > deltaY) { return false; } } return super.onInterceptTouchEvent(ev); } }
1.2.滑動慣性喪失
rvVertical.setNestedScrollingEnabled(false);
2.條目缺失問題
當第一個window不出現RecyclerView時,我們的顯示是沒問題的,反之,只能顯示一條,或兩條這個問題是同事發現的,我們當時都覺得很詭異另外,這是一個深坑,可能這個手機顯示沒問題,你換個高一點的手機,RecyclerView正好在第一個window露了頭,恩恩,結果就是,就剩那麼一兩條資料了這時,我們怎麼處理呢?
有兩種方案
2.1寫死條目高度,
網路請求獲得條目數量,根據條目數量,動態設定RecyclerView高度缺點:這時RecyclerView的高度是固定的,RecyclerView需要一次就把所有條目載入完,會造成recyclerview無法復 用,此時的recyclerview基本上就與LinearLayout無異,當條目數量很少時你可以這樣做,當條目數量很多時,無疑對效能是很大的傷害。
2.2推薦方法
在外層巢狀一個相對佈局即可
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
是不是解決了,這個其實是google的一個小坑
第一篇部落格,還希望大家少黑,給點鼓勵,分享是前進的動力,大家加油
qq:765472289 希望和大家一起探討