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);
}
}
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With