ScrollView嵌套RecyclerView、ListView时解决布局问题
来源:互联网 发布:淘宝netflix 编辑:程序博客网 时间:2024/06/03 23:47
scrollview 嵌套recyclerview 时,recyclerview不显示,这就需要我们自己计算recyclerview的高度,比如:
ViewGroup.LayoutParams mParams = recyclerView.getLayoutParams(); mParams.height = (CommonUtils.getScreenWidthPX(getActivity()) * 480 / 720 + CommonUtils.dipToPixels(40)) * num + CommonUtils.dipToPixels(8); mParams.width = CommonUtils.getScreenWidthPX(getActivity()); recyclerView.setLayoutParams(mParams);这中方法适合item高度比较好计算的情形,但要遇到里面的item高度不一定这就需要我们重写recyclerview的高度了。
重写recyclerView的方法:
public class MyRecyclerView extends RecyclerView{ public MyRecyclerView(Context context) { super(context); } public MyRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthSpec, int heightSpec) { int newHeightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(widthSpec, newHeightSpec); }}但是这种方法在recyclerview重写不管用。 我们此时要重写的的是LinearLayoutManager或GridLayoutManager
重写LinearLayoutManager
public class MyLinearLayoutManger extends LinearLayoutManager{ private static final String TAG = MyLinearLayoutManger.class.getSimpleName(); private int[] mMeasuredDimension = new int[2]; public MyLinearLayoutManger(Context context) { super(context); } public MyLinearLayoutManger(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public MyLinearLayoutManger(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { super.onMeasure(recycler, state, widthSpec, 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); KLog.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 动态添加时报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(); } }}
重写GridLayoutManager
public class FullyGridLayoutManager extends GridLayoutManager { private int mwidth = 0; private int mheight = 0; 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]; public int getMwidth() { return mwidth; } public void setMwidth(int mwidth) { this.mwidth = mwidth; } public int getMheight() { return mheight; } public void setMheight(int mheight) { this.mheight = mheight; } @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 % span == 0) { 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: } setMheight(height); setMwidth(width); 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);//fix 动态添加时报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(); } } }}
重写完之后,用就好说了,在adapter的onBindview和平常一样用就可以了
mLinearLayoutManager = new MyLinearLayoutManger(this);suggest_list_recycler.setLayoutManager(mLinearLayoutManager); //添加分隔线 RecyclerViewsuggest_list_recycler.addItemDecoration(new AdvanceDecoration(EventActivity.this, OrientationHelper.VERTICAL));suggest_list_recycler.setAdapter(adapter = new EventSuggestListAdapter(EventActivity.this, eventSuggestModel.list));
final FullyGridLayoutManager manager = new FullyGridLayoutManager(context.getActivity(), 3); manager.setOrientation(GridLayoutManager.VERTICAL); manager.setSmoothScrollbarEnabled(true); viewHolder.recyclerView.setLayoutManager(manager);
最后解决方法是重写最外层的Scrollview
** * 屏蔽 滑动事件 * Created by fc on 2015/7/16. */public class MyScrollview extends ScrollView { private int downX; 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 e) { int action = e.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downX = (int) e.getRawX(); downY = (int) e.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) e.getRawY(); if (Math.abs(moveY - downY) > mTouchSlop) { return true; } } return super.onInterceptTouchEvent(e); }}
这样就可以了,暴力屏蔽。。。。5以上的事件直接传递给了内层的recyclerview,所以我们把滑动事件拦截就好了。
===================================================================================
ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法:
参考:http://blog.csdn.net/spring_he/article/details/17409661
1.在类中添加setListViewHeightBasedOnChildren(ListView listView)方法;
/** * @param listview * 此方法是本次listview嵌套listview的核心方法:计算parentlistview item的高度。 * 如果不使用此方法,无论innerlistview有多少个item,则只会显示一个item。 **/ public void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); // 计算子项View 的宽高 listItem.measure(0, 0); // 统计所有子项的总高度 totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
2.在activity类中这样使用
<span style="font-family: Arial, Helvetica, sans-serif;">parentAdapter = new ParentAdapter(MainActivity.this,parentList);</span><pre name="code" class="java">act_listview.setAdapter(parentAdapter);setListViewHeightBasedOnChildren(act_listview);3.需要将activity中布局文件的listview和scrollview的宽高设置为match_parent;并将scrollview添加属性android:fillViewport = "true"。
布局activity_main如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <ListView android:id="@+id/act_listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView></LinearLayout>
4.将外层adapter和里层adapter的布局文件都已LinearLayout作为根布局。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/children_country_tv" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
总结:
1.在子listview的item最外面的一层布局LinearLayout 添加 android:descendantFocusability="blocksDescendants"属性;
2.在父listview中添加android:focusable="false" 属性;或者换种思路 listview中嵌入gridview,就不会出现无响应事件
android:descendantFocusability="blocksDescendants" //viewgroup会覆盖子类控件而直接获得焦点android:descendantFocusability="beforeDescendants" //viewgroup会优先其子类控件而获取到焦点 android:descendantFocusability="afterDescendants" //viewgroup只有当其子类控件不需要获取焦点时才获取焦点
1 0
- ScrollView嵌套RecyclerView、ListView时解决布局问题
- (4.3.2.17)Listview || ScrollView嵌套RecyclerView、ListView时解决布局问题
- ScrollView嵌套ListView,RecyclerView问题
- 解决ScrollView嵌套RecyclerView(横向)或ListView(横向)时,横向滑动不顺畅的问题。
- 解决ScrollView嵌套ListView问题
- 解决ScrollView嵌套ListView问题
- 解决ScrollView嵌套ListView问题
- 解决ScrollView嵌套ListView问题
- 解决ScrollView嵌套ListView问题
- 解决ScrollView嵌套ListView问题
- ScrollView嵌套ListView或者RecyclerView时默认滑动一段距离问题
- 解决ScrollView嵌套RecyclerView时,惯性滑动失效的问题
- 解决ScrollView嵌套RecyclerView时item显示不全的问题
- 解决ScrollView 和 RecyclerView 嵌套问题
- ScrollView 嵌套listview解决listview高度问题
- ScrollView嵌套recyclerView问题
- scrollview嵌套recyclerview 问题
- ScrollView嵌套RecyclerView 问题
- 表视图tableView的使用---摘自培训文档
- hadoop2.2环境配置
- iOS数据存储的常用方式
- Handler
- Tasks and Back Stack(任务和返回栈)
- ScrollView嵌套RecyclerView、ListView时解决布局问题
- 随记
- MySQL 之 Left out join
- Android View总结
- Root Pane Container(二)
- mysql设置联合主键的意义
- Sublime text3配置LiveReload 实时预览、无需刷新
- yum install gtk2-devel.x86_64
- Root Pane Container(三)