Android - 在 RecyclerView.Adapter 中更改附加到 ViewHolder 的 TextView 的文本大小

Android - changing textSize of TextView attached to ViewHolder in RecyclerView.Adapter

我在 RecyclerView 中使用 GridLayoutManager 来处理三种不同的情况(1 列、2 列、3 列),但我还需要更改 RecyclerView.Adapter 中附加到 ViewHolder 的 TextView 的 textSize。

Activity代码:

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);

编译器return 空引用。有什么想法吗?

看来你看错地方了。您不会向父 RecyclerView 询问每个子视图中的 TextView 的引用,您需要从每个单独的子视图中获取它。

看起来最简单的地方就是 onCreateViewHolder():

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.grid_layout, viewGroup, false);

    TextView text = (TextView) itemView.findViewById(R.id.titleTextViewGrid);
    text.setTextSize(12);
    return new ViewHolder(itemView);
}

...或者可能在 ViewHolder 创建期间:

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);

    vName.setTextSize(12);
}

这种方式是为每个项目视图设置的,但仅在视图的初始创建期间设置。

我找到了一个很好的解决方案,感谢Devunwired 的指导。当我查看时:

https://github.com/lucasr/twoway-view/blob/master/sample/src/main/java/org/lucasr/twowayview/sample/LayoutAdapter.java

他将 layoutId 传递给构造函数,这样我就可以调用新适配器了:

CustomAdapter mAdapter = new CustomAdapter(data, numberOfColumns);

然后在 CustomAdapter 中的 CustomViewHolder 中:

...
public class CustomViewHolder extends RecyclerView.ViewHolder {

    protected TextView vName;

    public CustomViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        vName = (TextView) itemLayoutView.findViewById(R.id.titleTextView);

        if (mLayoutId == 1) {
            vName.setTextSize(33);
        }
        else if (mLayoutId == 2) {
            vName.setTextSize(22);
        }
        else if (mLayoutId == 3) {
            vName.setTextSize(11);
        }
}
...