国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

關于Scrollview嵌套RecyclerView高度顯示不全

2019-11-06 09:56:26
字體:
來源:轉載
供稿:網友

前言

轉載請注明出處:http://blog.csdn.net/dreamsever/article/details/58117072

做 Android開發(fā)我們經常會遇到這種ScrollView嵌套Listview或者 嵌套RecyclerView的需求,當我們搜索解決方案時經常會遇到這句話

you should never put a scrollable view inside another scrollable view

雖然很多人不推薦這種方法,但是總有一些人(比如我)面對產品設計的一些奇葩界面不得不采用這種方案,今天我就把我找到的Scrollview嵌套Recyclerview高度顯示不全解決辦法總結一下。(ScrollView嵌套Listview的情況也是老生常談的話題,網上太多就不再贅述)

首先

網上已經有前人給我們寫出了一個自定義的LinearLayoutManager,我相信你也可以搜索到,下面我先粘貼出來,感興趣建議好好看看這個類的源碼,不要只做搬運工,知其然也要知其所以然,但是使用中會遇到一些小問題,下面講解

/** * 工具類 用于Scrollview嵌套recyclerview * 2017 2 17加入 */public class FullyLinearLayoutManager extends LinearLayoutManager { PRivate static final String TAG = FullyLinearLayoutManager.class.getSimpleName(); public FullyLinearLayoutManager(Context context) { super(context); } public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } private int[] mMeasuredDimension = new int[2]; @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { final int widthMode = View.MeasureSpec.getMode(widthSpec); final int heightMode = View.MeasureSpec.getMode(heightSpec); final int widthSize = View.MeasureSpec.getSize(widthSpec); final int heightSize = View.MeasureSpec.getSize(heightSpec); Log.i(TAG, "onMeasure called. /nwidthMode " + widthMode + " /nheightMode " + heightSpec + " /nwidthSize " + widthSize + " /nheightSize " + heightSize + " /ngetItemCount() " + getItemCount()); int width = 0; int height = 0; for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), mMeasuredDimension); if (getOrientation() == HORIZONTAL) { width = width + mMeasuredDimension[0]; if (i == 0) { height = mMeasuredDimension[1]; } } else { height = height + mMeasuredDimension[1]; if (i == 0) { width = mMeasuredDimension[0]; } } } switch (widthMode) { case View.MeasureSpec.EXACTLY: width = widthSize; case View.MeasureSpec.AT_MOST: case View.MeasureSpec.UNSPECIFIED: } switch (heightMode) { case View.MeasureSpec.EXACTLY: height = heightSize; case View.MeasureSpec.AT_MOST: case View.MeasureSpec.UNSPECIFIED: } setMeasuredDimension(width, height); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { try { View view = recycler.getViewForPosition(0);//fix 動態(tài)添加時報IndexOutOfBoundsException if (view != null) { RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width); int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height); view.measure(childWidthSpec, childHeightSpec); measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; recycler.recycleView(view); } } catch (Exception e) { e.printStackTrace(); } finally { } }}

小問題,值得注意的地方

1,

給scrollview加入一個屬性

android:fillViewport="true"

2,

處理滑動沖突情況,如下設置

recyclerView.setNestedScrollingEnabled(false);

3,

我開始用的scrollview,測試設備:小米note黑 adnroid6.0,由于我的里面有兩個RecyclerView我發(fā)現下面的超出屏幕的部分就不顯示了,其他的機型是可以的。網上搜索建議將Scrollview換成android.support.v4.widget.NestedScrollView,我試了一下確實正常工作了

總結

you should never put a scrollable view inside another scrollable view

如果這個界面確實要求不高,這樣寫確實省了不少功夫,那就這樣干吧!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 循化| 桦川县| 三穗县| 大荔县| 三原县| 新密市| 都匀市| 页游| 鹤庆县| 永宁县| 澳门| 丰台区| 农安县| 巴彦县| 广丰县| 大城县| 平昌县| 百色市| 延吉市| 阜城县| 莱阳市| 茂名市| 贡山| 闸北区| 曲阜市| 长垣县| 渝中区| 吐鲁番市| 左权县| 德昌县| 策勒县| 景宁| 疏勒县| 浦北县| 类乌齐县| 拉萨市| 永年县| 太白县| 商水县| 彰化市| 连平县|