第一种:在你的RecyclerView上外边嵌套一层RelativeLayout,
然后添加属性 android:descendantFocusability="blocksDescendants",
如:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_meeting"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
PS:既然提到了这个属性就说下它的意思吧,知道的同学再复习一遍呗,巩固巩固更牢靠。
属性android:descendantFocusability的含义是:当一个view获取焦点时,定义ViewGroup和其子控件两者之间的关系。
它一共有3个属性值,它们分别是:
beforeDescendants:viewGroup会优先子类控件而获取焦点
afterDescendants:viewGroup只有当子类控件不需要获取焦点的时候才去获取焦点
blocksDescendants:viewGroup会覆盖子类控件而直接获取焦点
第二种方法:
首先在xml布局中将你的ScrollView替换成android.support.v4.widget.NestedScrollView,
并在java代码中设置recyclerView.setNestedScrollingEnabled(false);
第三种 代码 使用场景 多种条目展示
/** * sgf * 最大化的RecyclerView,嵌套于ScrollView之中使用,处理多个RecyclerView显示不全的问题 */ public class MaxRecyclerView extends RecyclerView { public MaxRecyclerView(android.content.Context context, android.util.AttributeSet attrs){ super(context, attrs); } public MaxRecyclerView(android.content.Context context){ super(context); } /** * 设置不滚动 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
第四种 使用场景 单条目展示 通过动态计算Item的高度来设置RecyView本身
将recyclerView 传入Adapter中
HomeAdapter homeAdapter = new HomeAdapter(getActivity(), datas,recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(content)); recyclerView.setNestedScrollingEnabled(false); recyclerView.setAdapter(homeAdapter);Adapter中
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
setRecyclerViewHeight();
}/**
* 1、获取ItemView的高度
* 2、itemView的数量
* 3、使用 itemViewHeight * itemViewNum = RecyclerView的高度
*/
private void setRecyclerViewHeight () {if (isCalcaulationRvHeight || mRv == null) return;
isCalcaulationRvHeight = true;
// 获取ItemView的高度
RecyclerView.LayoutParams itemViewLp = (RecyclerView.LayoutParams) mItemView.getLayoutParams();
// itemView的数量
int itemCount = getItemCount();
// 使用 itemViewHeight * itemViewNum = RecyclerView的高度
int recyclerViewHeight = itemViewLp.height * itemCount;
// 设置RecyclerView高度
LinearLayout.LayoutParams rvLp = (LinearLayout.LayoutParams) mRv.getLayoutParams();
rvLp.height = recyclerViewHeight;
mRv.setLayoutParams(rvLp);
}