【问题标题】:Android - changing textSize of TextView attached to ViewHolder in RecyclerView.AdapterAndroid - 在 RecyclerView.Adapter 中更改附加到 ViewHolder 的 TextView 的 textSize
【发布时间】: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


【解决方案1】:

看来您找错地方了。您不会向父级 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);
}

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

【讨论】:

  • 你是对的,但是如何从Activity告诉适配器,GridLayoutManager中的列数发生了变化?
【解决方案2】:

我找到了一个很好的解决方案,感谢 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);
        }
}
...

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多