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 方法中重新設(shè)置 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);
}
}