RecyclerView
Android RecyclerView is basically a more advanced and flexible version of ListView. It displays large datasets more efficiently, more customizable and allows for better memory management and performance.
- The views can be recycled.
- Views can be arranged horizontally, vertically or as grid or staggered mode.
- LayoutManager can be used to position items as desired.
- LinearLayoutManager shows items in a vertical or horizontal scrolling list.
- GridLayoutManager shows items in a grid.
- StaggeredGridLayoutManager shows items in a staggered grid.
- It needs ViewHolder in contrast to ListView which may or may not.
- RecyclerView.ItemAnimator will animate ViewGroup modifications such as add/delete/select that are notified to adapter.
Main Advantages
- Add RecyclerView support library to the gradle build file.
- Define a model class to use as the data source.
- Add a RecyclerView to your activity to display the items.
- Every adapter has three primary methods: onCreateViewHolder to inflate the item layout and create the holder, onBindViewHolder to set the view attributes based on the data and getItemCount to determine the number of items. All three need to be implemented to finish the adapter.
- Unlike ListView, there is no way to add or remove items directly through the RecyclerView adapter. Changes can be made to the data source directly and adapter can be notified.
- Create a custom row layout XML file to visualize the item.
- Create a RecyclerView.Adapter and ViewHolder to render the item.
- Bind the adapter to the data source to populate the RecyclerView.
- Arraylist can be used to create list of items to be displayed.
- Hashmap can be used for key value pairs. 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 layout file for individual item in 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 reference to individual view
4. Create an adapter class to manage the data and view binding for 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); } }