Sobes.tech
Junior — Middle

Do you have experience using the RecyclerView component in Android?

sobes.tech AI

Answer from AI

Yes, the RecyclerView component in Android is used for efficiently displaying large lists or collections of data with the ability to reuse interface elements.

Main points:

  • RecyclerView works with an adapter that binds data to list items.
  • Uses ViewHolder to optimize performance by minimizing calls to findViewById.
  • Allows easy implementation of various layout types (linear list, grid, etc.) through LayoutManager.

Example of a simple adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> data;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public ViewHolder(View v) {
            super(v);
            textView = v.findViewById(R.id.textView);
        }
    }

    public MyAdapter(List<String> data) {
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}