关于Recyclerview的item指定高度却不对的问题

来源:互联网 发布:最可信的网络兼职 编辑:程序博客网 时间:2024/05/17 02:12

(本文说的不是ScrollView嵌套Recyclerview的问题哈)

关于recyclerview的item布局高度问题:

 

参照:

http://stackoverflow.com/questions/30226298/how-to-have-variable-row-height-in-recyclerview

 

每一个item的布局(test_item.xml):

<?xml version="1.0"encoding="utf-8"?>
<FrameLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="200dp"
   
android:background="@color/grep"
   
>

    <TextView
       
android:id="@+id/test_text"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:layout_gravity="center"
       
android:gravity="center"
       
android:text="hello"
       
android:textSize="30sp"/>
</FrameLayout>

 

外面那层设置了200dp

 

其他没什么特别的,主要是adapter的代码

public class testAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {    private ArrayList<String> mList;    public testAdapter() {        mList = new ArrayList<>();    }    public void addData(ArrayList<String> list) {        mList.addAll(list);        notifyDataSetChanged();    }    public void setData(ArrayList<String> list) {        mList.clear();        mList.addAll(list);        notifyDataSetChanged();    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        return new testVH(LayoutInflater.from(testActivity.this).inflate(R.layout.test_item, null));    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {        ((testVH) holder).setData(mList.get(position));    }    @Override    public int getItemCount() {        return mList.size();    }}

 

 

这样搞的话,会导致recyclerview每一个item的高度和宽度只是适应textview的高度和宽度,而不是我们设定的200dp。

问题出在onCreateViewHolder里面的

return new testVH(LayoutInflater.from(testActivity.this).inflate(R.layout.test_item, null));

 

主要是红字部分:

改成:

return new testVH(LayoutInflater.from(testActivity.this).inflate(R.layout.test_item, parent, false));

 

即可解决,然后就是我们想要的200dp的item高度了。

0 0
原创粉丝点击