RecycleView设置屏幕显示固定条数据
只需在RecycleView的Adapter中onCreateViewHolder方法里实现下列代码即可
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_line_info, parent, false); MyViewHolder viewHolder = new MyViewHolder(view); int parentHeight = parent.getHeight(); parent.getWidth(); ViewGroup.LayoutParams layoutParams = viewHolder.mLayoutContainer.getLayoutParams(); layoutParams.height = parentHeight / 5; return viewHolder; }
需要注意的是:
1、inflate需要调用这个函数
inflate(int resource, ViewGroup root, boolean attachToRoot)
2、parentHeight / 5 表示屏幕显示5条item,当然你也可以改成你需要显示的条数。