How to use the Android adapter RecycleView
The steps for using a RecyclerView adapter are as follows:
- Create an adapter class that inherits from RecyclerView.Adapter and implements the following methods:
- onCreateViewHolder(): create a ViewHolder object and return it
- Bind data to the ViewHolder object in the onBindViewHolder() method.
- getItemCount(): returns the size of the data collection
- Create a ViewHolder class that extends RecyclerView.ViewHolder, and initialize the views in the layout in the constructor.
- Initialize the RecyclerView and set the adapter in the Activity or Fragment.
- Create a LayoutManager object to set the layout style of the RecyclerView (such as LinearLayoutManager, GridLayoutManager, etc.).
- Create an adapter object and pass the data set to the adapter’s constructor.
- Invoke the setLayoutManager() method of RecyclerView to set the LayoutManager.
- Use the method setAdapter() of RecyclerView to set the adapter.
- Implement the methods in the adapter.
- In the onCreateViewHolder() method, the layout file is converted to a View object using LayoutInflater.from(context) and a ViewHolder object is created and returned.
- In the onBindViewHolder() method, fetch the corresponding data object based on the position, and then bind the data to the views of the ViewHolder object.
- getItemCount(): returns the size of the data collection
- Optional: Add features such as divider lines and click events to the RecyclerView.
Here is a basic example code:
Adapter class:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> data;
public MyAdapter(List<String> data) {
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String item = data.get(position);
holder.textView.setText(item);
}
@Override
public int getItemCount() {
return data.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
}
}
}
Using adapters in Activity:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter adapter;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
data = new ArrayList<>();
// 添加数据到data集合中
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);
}
}
In the above code, R.layout.item_layout is the layout file in the adapter, which includes a TextView to display data. In the onBindViewHolder() method, the data from the data collection is bound to the textView of the ViewHolder object. In the Activity, the RecyclerView layout is set with LinearLayoutManager, and an adapter object is created and set to the RecyclerView. Updating the data display of the RecyclerView can be done by calling adapter.notifyDataSetChanged() method.