简体   繁体   中英

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

I use GridLayoutManager in RecyclerView for three different situation (1 column, 2 columns, 3 columns), but I also need change textSize of TextView attached to ViewHolder in RecyclerView.Adapter.

Activity code:

mLayoutManager = new GridLayoutManager(this, numberOfColumns);

mRecyclerView = (RecyclerView) findViewById(R.id.grid);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new CustomGridAdapter(data);
mRecyclerView.setAdapter(mAdapter);

Adapter:

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

    }
}

When i try:

TextView text = (TextView) mRecyclerView.findViewById(R.id.titleTextView);
text.setTextSize(12);

Compiler return null reference. Any idea please?

It looks like your looking in the wrong place. You wouldn't ask the parent RecyclerView for a reference to the TextView in each child, you need to get that from each individual child view.

It would seem like the simplest place to do so would be inside of 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);
}

...or perhaps during the ViewHolder creation:

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

This way it is set for each item view, but only during initial creation of the view.

I found a very good solution, thank you Devunwired for the direction. When I look to:

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

He pass layoutId to the constructor, so then i can call the new adapter:

CustomAdapter mAdapter = new CustomAdapter(data, numberOfColumns);

and then in CustomViewHolder in CustomAdapter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM