How to use RecyclerView in an Android app?

A RecyclerView is a more advanced and flexible version of ListView in Android. It allows you to display a large set of data efficiently while saving memory. Using a RecyclerView, you can create complex lists with great performance.
RecyclerView, Android development, ListView, memory efficiency, data display, Kotlin, Java
// Step 1: Add RecyclerView dependency in your build.gradle file dependencies { implementation 'androidx.recyclerview:recyclerview:1.2.1' } // Step 2: Create a layout for RecyclerView in XML (res/layout/activity_main.xml) // Step 3: Create a ViewHolder class to hold the view elements public class MyViewHolder extends RecyclerView.ViewHolder { public TextView textView; public MyViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.textView); } } // Step 4: Create an Adapter class for RecyclerView public class MyAdapter extends RecyclerView.Adapter { private List dataList; public MyAdapter(List dataList) { this.dataList = dataList; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_layout, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.textView.setText(dataList.get(position)); } @Override public int getItemCount() { return dataList.size(); } } // Step 5: Setup RecyclerView in your Activity (MainActivity.java) public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recyclerView); List myData = new ArrayList<>(); myData.add("Item 1"); myData.add("Item 2"); myData.add("Item 3"); MyAdapter adapter = new MyAdapter(myData); recyclerView.setAdapter(adapter); } }

RecyclerView Android development ListView memory efficiency data display Kotlin Java