How does the RecyclerView concept work on Android?

I created a basic application using RecyclerView and CardView from tutorials from websites.

The application is working fine and I have some confusion. (I show all my code here)

the confusion is how the code works step by step. so please clarify my concept.

The main structure of my application:

  1. I created the xml file row_data_layout to bind to recycler_view .
  2. A data class file was created (here I defined my variable, which I used in the application)
  3. An adapter file has been created (here I want to clarify how it works step by step, which class is called, and why?).
  4. Bind data to RecyclerView in MainActivity file

File row_data_layout.xml :

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/CardView" android:paddingBottom="16dp" android:layout_marginBottom="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> </android.support.v7.widget.CardView> 

Data Class File:

 public class Data { public String Name; Data(String Name) { this.Name=Name; } } 

Class Data_Adapter File:

 public class Data_Adapter extends RecyclerView.Adapter<Data_Adapter.View_holder> { List<Data> list = Collections.emptyList(); Context context; public Data_Adapter(List<Data> list, Context context) { this.list = list; this.context = context; } @Override public Data_Adapter.View_holder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_data_layout,parent,false); View_holder holder=new View_holder(v); return holder; } @Override public void onBindViewHolder(Data_Adapter.View_holder holder, int position) { holder.name.setText(list.get(position).Name); } @Override public int getItemCount() { return list.size(); } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } public class View_holder extends RecyclerView.ViewHolder{ CardView cv; TextView name; public View_holder(View itemView) { super(itemView); cv = (CardView) itemView.findViewById(R.id.CardView); name = (TextView) itemView.findViewById(R.id.txt_name); } } } 

MainActivity File:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<Data> data = fill_data(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); Data_Adapter adapter = new Data_Adapter(data,getApplicationContext()); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } public List<Data> fill_data() { List<Data> data = new ArrayList<>(); data.add(new Data("Bred Pit")); data.add(new Data("Leonardo")); return data; } } 
+6
source share
1 answer

Once you have a common understanding of how RecyclerView.Adapter works, it would be wise to dive deeper into the documentation .

What the adapter does is to keep the pool of bloated views (it can be as many different types of ViewHolder as you would like) to populate with the data that you supply. When the adapter does not have an empty representation in the pool, it creates a new one.

When a view is tied to a RecyclerView, it is removed from the pool, and when it is disconnected (scrolls out of the view, by some distance), it is added back to the pool of empty views - this is why it is important to reset everything when you populate your ViewHolders.

The onCreateViewHolder () function is to create a new empty view (completed by RecyclerView.ViewHolder) and add it to the pool.

The onBindViewHolder () function retrieves the view from the empty pool and populates this view using the data that you provided to the adapter. \

You can use the onViewRecycled () method to perform certain actions, such as setting the ImageView bitmap to null (when disconnected) to reduce memory usage.

I usually do not override onAttachedToRecyclerView () , but if you need to do something specific when your adapter is connected to RecyclerView, you do it here.

+27
source

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


All Articles