Why use static with RecyclerView.ViewHolder

Why it is recommended to use staticfor classextended from RecyclerView.ViewHolderif I create a new instance of this class in a method onCreateViewHolder, and I think this instance is used for each element:

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,parent,false);
    return new RecyclerViewAdapter.RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {

    String textTop = noticias.get(position).getHora()+ noticias.get(position).getTemperatura();

    holder.textViewTop.setText(textTop);
    holder.textViewBot.setText(noticias.get(position).getTexto());

}


public static class RecyclerViewHolder extends RecyclerView.ViewHolder{

    public TextView textViewTop;
    public TextView textViewBot;

    public RecyclerViewHolder(View view){
        super(view);
        textViewTop = (TextView) view.findViewById(R.id.textView4);
        textViewBot = (TextView) view.findViewById(R.id.textView5);
    }

}
+4
source share
1 answer

The inner class contains a reference to the outer class. Thus, this means that each instance of yours RecyclerView.ViewHolderwill contain a link to yours RecyclerView.Adapter.

By doing this static, you avoid saving this link.

Oracle Java Documentation - Nested Classes

+13

Source: https://habr.com/ru/post/1615833/


All Articles