美文网首页
RecyclerView Item 中宽度 match_pare

RecyclerView Item 中宽度 match_pare

作者: Passon_Fang | 来源:发表于2017-11-03 13:10 被阅读90次

item 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tv_center_choice_item"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:textColor="#222222"
    tools:text="temp"
    android:textSize="16sp"/>

解决方法: 在 onBind 方法中重新设置 TextView 的宽度。

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int position, List<CenterModel> mDatas) {
        ChoiceVH holder = (ChoiceVH) viewHolder;
        holder.mTv.setText(" ");

        ViewGroup.LayoutParams layoutParams = holder.mTv.getLayoutParams();
        layoutParams.width = mContext.getResources().getDisplayMetrics().widthPixels;
        Log.d(TAG, "onCreateVH: onBind.width" + layoutParams.width);
        holder.mTv.setLayoutParams(layoutParams);

        if (position > 1 && position < mDatas.size() + 2) {
            CenterModel data = mDatas.get(position - 2);
            holder.mTv.setText(data.name);
        }
    }

相关文章

网友评论

      本文标题:RecyclerView Item 中宽度 match_pare

      本文链接:https://www.haomeiwen.com/subject/alltmxtx.html