Android中RecyclerView與Scrollview組合使用(二)
1、LinearLayoutManager和ScrollView巢狀
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);
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 {
}
}
}
2、GridLayoutManager和ScrollView進行巢狀
public class FullyGridLayoutManager extends GridLayoutManager {
public FullyGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, 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);
int width = 0;
int height = 0;
int count = getItemCount();
int span = getSpanCount();
for (int i = 0; i < count; i++) {
measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
if (i % span == 0) {
width = width + mMeasuredDimension[0];
}
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
if (i == 0) {
width = mMeasuredDimension[0];
}
if (i % span == 0) {
height = height + mMeasuredDimension[1];
}
}
}
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) {
if (position < getItemCount()) {
try {
View view = recycler.getViewForPosition(0);
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();
}
}
}
}
3、StaggeredGridLayoutManager和ScrollView進行巢狀
public class FullyStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
public FullyStaggeredGridLayoutManager(int spanCount, int orientation) {
super(spanCount, orientation);
}
// 尺寸的陣列,[0]是寬,[1]是高
private int[] measuredDimension = new int[2];
// 用來比較同行/列那個item罪寬/高
private int[] dimension;
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
// 寬的mode+size
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
// 高的mode + size
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
// 自身寬高的初始值
int width = 0;
int height = 0;
// item的數目
int count = getItemCount();
// item的列數
int span = getSpanCount();
// 根據行數或列數來建立陣列
dimension = new int[span];
for (int i = 0; i < count; i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredDimension);
// 如果是豎直的列表,計算item的高,否則計算寬度
//Log.d("LISTENER", "position " + i + " height = " + measuredDimension[1]);
if (getOrientation() == VERTICAL) {
dimension[findMinIndex(dimension)] += measuredDimension[1];
} else {
dimension[findMinIndex(dimension)] += measuredDimension[0];
}
}
if (getOrientation() == VERTICAL) {
height = findMax(dimension);
} else {
width = findMax(dimension);
}
switch (widthMode) {
// 當控制元件寬是match_parent時,寬度就是父控制元件的寬度
case View.MeasureSpec.EXACTLY:
width = widthSize;
break;
case View.MeasureSpec.AT_MOST:
break;
case View.MeasureSpec.UNSPECIFIED:
break;
}
switch (heightMode) {
// 當控制元件高是match_parent時,高度就是父控制元件的高度
case View.MeasureSpec.EXACTLY:
height = heightSize;
break;
case View.MeasureSpec.AT_MOST:
break;
case View.MeasureSpec.UNSPECIFIED:
break;
}
// 設定測量尺寸
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
// 挨個遍歷所有item
if (position < getItemCount()) {
try {
View view = recycler.getViewForPosition(position);//fix 動態新增時報IndexOutOfBoundsException
if (view != null) {
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), lp.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), lp.height);
// 子view進行測量,然後可以通過getMeasuredWidth()獲得測量的寬,高類似
view.measure(childWidthSpec, childHeightSpec);
// 將item的寬高放入陣列中
measuredDimension[0] = view.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
recycler.recycleView(view);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private int findMax(int[] array) {
int max = array[0];
for (int value : array) {
if (value > max) {
max = value;
}
}
return max;
}
/**
* 得到最陣列中最小元素的下標
*
* @param array
* @return
*/
private int findMinIndex(int[] array) {
int index = 0;
int min = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
index = i;
}
}
return index;
}
}
4、重寫ScrollView
public class MyScrollview extends ScrollView {
private int downY;
private int mTouchSlop;
public MyScrollview(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public MyScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) event.getRawY();
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(event);
}
}
5、注意事項
(1)RecyclerView外層要巢狀RelativeLayout
Android中RecyclerView的下拉重新整理和上拉載入使用(三)
相關文章
- Android使用RecyclerView實現二級列表AndroidView
- 索引與null(二):組合索引索引Null
- Android 普通View截圖 RecyclerView截圖 ScrollView截圖分享AndroidView
- Android Intent Flag組合使用AndroidIntent
- Android中的RecyclerViewAndroidView
- Android RecyclerView的使用AndroidView
- 在Android與recyclerview中使用列表和網格AndroidView
- Java中的繼承與組合Java繼承
- 解決RecyclerView在ScrollView中會置頂它上面的控制元件View控制元件
- 【Android進階】RecyclerView之快取(二)AndroidView快取
- Android RecyclerView 簡介與例項AndroidView
- android listview and scrollviewAndroidView
- 解決ScrollView巢狀RecyclerView滑動卡頓問題View巢狀
- Android RecyclerView中Adapter和ViewHoAndroidViewAPT
- Oracle中組合索引的使用詳解Oracle索引
- 組合數學筆記-排列與組合筆記
- RecyclerView使用指南(二)—— 多種ItemLayoutView
- ScrollView巢狀RecyclerView滑動衝突相關問題View巢狀
- Android中的RecyclerView: 基礎知識AndroidView
- 繼承與組合繼承
- RecyclerView使用封裝與優化View封裝優化
- Android入門教程 | RecyclerView使用入門AndroidView
- Android入門教程 | RecyclerView實際使用AndroidView
- Android中ScrollView實現拖拽反彈效果動畫AndroidView動畫
- android之截圖(包括擷取scrollview與listview的)AndroidView
- RecyclerView使用View
- 第二章--組合語言組合語言
- Android Webview和ScrollView衝突和WebView使用總結AndroidWebView
- PHP專案中composer和Git的組合使用PHPGit
- ORACLE 組合索引 使用分析Oracle索引
- Android RecyclerView詳解AndroidView
- Android中RecyclerView用法,一步一步教你如何使用RecyclerView以及帶你走過編碼中可能會出現的坑~AndroidView
- 解決 ScrollView 巢狀 RecyclerView 時,慣性滑動失效的問題View巢狀
- 組合模式-統一的處理個別物件與組合物件模式物件
- 組合語言-學習記錄(二)組合語言
- 第二階段複習——組合
- 教你玩轉 Android RecyclerView:深入解析 RecyclerView ItemDecoration類AndroidView
- RecyclerView的使用View