Android - 在RecyclerView.Adapter中更改附加到ViewHolder的TextView的textSize

时间:2015-02-19 03:59:40

标签: android adapter textview layout-manager android-recyclerview

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

编译器返回null引用。好吗?

2 个答案:

答案 0 :(得分:0)

看起来你在错误的地方看。您不会要求父母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);
}

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

答案 1 :(得分:0)

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