Android Recycler View解决item宽度无法填充屏幕(没有父容器参数也行)

在网上看了很多关于如何解决RecyclerView 的item填充屏幕的文章,但是看到的那些文章几乎方法都一样,还是无法解决到我的问题。但是看了那么多文章之后终于从一篇文章中找到了灵感,地址是:RecyclerView Item 布局宽高无效问题探究。这篇文章中探讨了为什么item无法填充屏幕的原因。非常感谢这位作者!
废话我就不多说 ,进入正题。我们获得一个布局的时候 无非就是用inflate方法,这个 方法有四个重载方法,一般来说我用的比较多的就是.inflate(R.layout.xx, parent, false);和.inflate(R.layout.xx,null)这两个方法,当我遇到item无法填充屏幕宽度的时候正是使用了第二个方法,于是我测试了第一个方法,解决了问题。通常我在使用Recycler View时,Item布局出问题,我就把两个方法都试过(前提是布局没问题),一般都可以解决掉,但是今天我要给RecyclerView添加一个HeaderView的时候,出现了HeaderView无法填充屏幕宽度的情况。我只能使用上面提到的方法中的第二种方法,因为我不知道怎么拿到父容器(我是菜鸟),具体代码如下

public void setFooterView(int resId) {
        this.headerView=LayoutInflater.from(mContext).inflate(resId,null);
        notifyItemInserted(0);
    }
在这个时候我也没办法解决,我也很绝望,所以我不停的去网上看文章,后来看到了前面提到的那篇文章,才知道,假如使用.inflate(R.layout.xx,null)加载布局,没有父容器,得到的布局的宽高都是wrap_content的(不清楚的同学可以点链接进去看原因)。所以我在xml文件中怎么写宽度和高度都是无效的。于是我想到了一个很烂的办法解决这个问题。办法如下:
解决问题的代码:
<?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="wrap_content"
    android:background="@color/grey"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/middle_margin"
        android:layout_marginTop="@dimen/small_marginII"
        android:text="热门城市"
        android:textColor="@color/black"
        android:textSize="@dimen/bigII_text_size" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_hotCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/middle_margin"
        android:layout_marginRight="@dimen/middle_margin"
        android:layout_marginTop="@dimen/small_marginII">

    </android.support.v7.widget.RecyclerView>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginBottom="@dimen/small_margin"
        android:layout_marginTop="@dimen/small_margin"
        android:background="@color/textgrey" />

</LinearLayout>    

这段代码中大家可以看到末尾处有一个View,解决问题的关键就在这个View上面。我的想法是:既然你不管怎么样都是wrap_content,那么我就把你撑到match_parent,于是我将View的宽度设置成match_parent,再次运行,发现我的headerview可以填充了。至此解决了我的问题。可能有人有疑惑为何我上面的textView是match_parent为何无法填充,具体我也不是很清楚,但是我觉得应该是它只测量字体所占宽度,才会导致这样的效果吧。这是我目前知道的办法,大神勿喷,如果谁有更好的办法希望能告知一下,不胜感激。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以在RecyclerView的适配器中给item view设置单击事件监听器。具体可以参考下面的代码: ``` public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<String> mDataList; private OnItemClickListener mListener; public MyAdapter(List<String> dataList, OnItemClickListener listener) { mDataList = dataList; mListener = listener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { String data = mDataList.get(position); holder.bindData(data); } @Override public int getItemCount() { return mDataList.size(); } public interface OnItemClickListener { void onItemClick(int position); } public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.text_view); itemView.setOnClickListener(this); } public void bindData(String data) { mTextView.setText(data); } @Override public void onClick(View v) { if (mListener != null) { int position = getAdapterPosition(); mListener.onItemClick(position); } } } } ``` 在Activity或Fragment中设置单击事件监听器: ``` MyAdapter adapter = new MyAdapter(dataList, new MyAdapter.OnItemClickListener() { @Override public void onItemClick(int position) { // 处理单击事件 } }); recyclerView.setAdapter(adapter); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值