【发布时间】:2015-02-19 03:59:40
【问题描述】:
我在 RecyclerView 中使用 GridLayoutManager 处理三种不同的情况(1 列、2 列、3 列),但我还需要更改附加到 RecyclerView.Adapter 中 ViewHolder 的 TextView 的 textSize。
活动代码:
mLayoutManager = new GridLayoutManager(this, numberOfColumns);
mRecyclerView = (RecyclerView) findViewById(R.id.grid);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CustomGridAdapter(data);
mRecyclerView.setAdapter(mAdapter);
适配器:
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.grid_layout, viewGroup, false);
return new ViewHolder(itemView);
}
...
public class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
protected TextView vName;
protected ImageView vImage;
protected ImageButton vButton;
protected ProgressBar vProgress;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
vName = (TextView) itemLayoutView.findViewById(R.id.titleTextViewGrid);
vImage = (ImageView) itemLayoutView.findViewById(R.id.imageView);
vButton = (ImageButton) itemLayoutView.findViewById(R.id.action_edit_grid);
vProgress = (ProgressBar) itemLayoutView.findViewById(R.id.loadingProgressBar);
}
}
当我尝试时:
TextView text = (TextView) mRecyclerView.findViewById(R.id.titleTextView);
text.setTextSize(12);
编译器返回空引用。请问有什么办法吗?
【问题讨论】:
-
你能分享你的适配器代码
-
在哪里尝试使用
TextView text = (TextView) mRecyclerView.findViewById(R.id.titleTextView);代码? -
在Activity中,初始化recyclerView后,GridLayoutManager的列数变化在哪里:GridLayoutManager(this, numberOfColumns)。
标签: android adapter textview layout-manager android-recyclerview