Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use static with RecyclerView.ViewHolder [duplicate]

Why is recommended to use static for a class extended from RecyclerView.ViewHolder if I create a new instance of this class on the onCreateViewHolder method and I guess that instance is used for each item:

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

}
like image 673
marco2704 Avatar asked Nov 13 '15 21:11

marco2704


People also ask

Why do we use ViewHolder in Android?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.

Why does RecyclerView repeat?

RecyclerView asks the adapter to create a new list item view for the first data item in your list. Once it has the view, it asks the adapter to provide the data to draw the item. This process repeats until the RecyclerView doesn't need any more views to fill the screen.

Which of the following are advantages to using RecyclerView?

RecyclerView lets you use packages to organize your code. RecyclerView helps save processing time, which can help scrolling through a list smoother. RecyclerView is designed to be efficient for lists by reusing views that have scrolled off the screen. RecyclerView automatically incorporates Material Design components.

What is the difference between onCreateViewHolder and onBindViewHolder?

This method internally calls onBindViewHolder to update the ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView. This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView.


1 Answers

Inner class contains reference to the outer class. So it means that every instance of your RecyclerView.ViewHolder will contain reference to your RecyclerView.Adapter.

By making it static you avoid keeping this reference.

Oracle Java Documentation - Nested Classes

like image 172
Damian Kozlak Avatar answered Oct 02 '22 12:10

Damian Kozlak