RecyclerView
Android RecyclerView is a more advanced and flexible version of ListView. It is designed to display large datasets more efficiently and with better memory management. RecyclerView is highly customizable, offering better performance compared to traditional ListViews.
- Views can be recycled to optimize memory usage.
- Views can be arranged horizontally, vertically, in a grid, or in a staggered layout.
- A LayoutManager is used to position items in the RecyclerView as needed.
- The LinearLayoutManager shows items in a vertical or horizontal scrolling list.
- The GridLayoutManager displays items in a grid format.
- The StaggeredGridLayoutManager displays items in a staggered grid layout.
- RecyclerView requires a ViewHolder, unlike ListView which may or may not use one.
- RecyclerView.ItemAnimator handles animations for item changes such as additions, deletions, or selections.
Main Advantages
- Add the RecyclerView support library to the gradle build file.
- Define a model class that represents the data source.
- Add a RecyclerView to your activity to display the items.
- Implement the three primary methods in the adapter:
onCreateViewHolder to inflate the item layout and create the ViewHolder,
onBindViewHolder to bind data to the views, and
getItemCount to return the number of items. - Unlike ListView, you cannot directly add or remove items through the RecyclerView adapter. Changes are made to the data source, and the adapter is notified.
- Create a custom row layout XML file to define how each item is displayed.
- Create a RecyclerView.Adapter and ViewHolder to manage item rendering.
- Bind the adapter to the data source to populate the RecyclerView.
- Use an ArrayList to create a list of items for display.
- A HashMap can be used for key-value pairs in the dataset. 1. Add RecyclerView to the layout XML file:
Steps to Add RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
2. Create a layout file for individual items in the RecyclerView:
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="24sp" />
3. Create a ViewHolder class to hold references to individual views:
4. Create an adapter class to manage data and bind views for the RecyclerView:public class MyViewHolder extends RecyclerView.ViewHolder { public TextView textView; public MyViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.item_text); } }
5. Initialize the RecyclerView in the activity and set the adapter:public class MyAdapter extends RecyclerView.Adapter{ private List itemList; public MyAdapter(List itemList) { this.itemList = itemList; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_layout, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { String item = itemList.get(position); holder.textView.setText(item); } @Override public int getItemCount() { return itemList.size(); } }
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recycler_view); ListitemList = Arrays.asList("Item 1", "Item 2", "Item 3", "Item 4", "Item 5"); adapter = new MyAdapter(itemList); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); } }