RecyclerView _ dynamic lists
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they’re needed.
As the name implies, RecyclerView recycles those individual elements. When an item scrolls off the screen, RecyclerView doesn’t destroy its view. Instead, RecyclerView reuses the view for new items that have scrolled onscreen. This reuse vastly improves performance, improving your app’s responsiveness and reducing power consumption.
https://developer.android.com/guide/topics/ui/layout/recyclerview
Sample pictures
Java code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.example.recyclerview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ArrayList<Contact> contacts; RecyclerView recyclerView; ContactAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.rv_main); contacts = Contact.createContactsList(500); adapter = new ContactAdapter(contacts); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.example.recyclerview; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.TextView; import java.util.ArrayList; public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyViewHolder> { private ArrayList<Contact> contacts; public class MyViewHolder extends RecyclerView.ViewHolder { public TextView name; public CheckBox chBox; public MyViewHolder(@NonNull View itemView) { super(itemView); name = itemView.findViewById(R.id.rv_text); chBox = itemView.findViewById(R.id.rv_chk); } } public ContactAdapter(ArrayList<Contact> contacts) { this.contacts = contacts; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { View itemView = LayoutInflater.from(viewGroup.getContext()). inflate(R.layout.recycler_view, viewGroup, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) { Contact contact = contacts.get(position); myViewHolder.name.setText(contact.get_name()); myViewHolder.chBox.setChecked(contact.get_state()); } @Override public int getItemCount() { return contacts.size(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.example.recyclerview; import java.util.ArrayList; public class Contact { private String _name; private boolean _state; public Contact(String name, boolean state) { _name = name; _state = state; } public String get_name() { return _name; } public boolean get_state() { return _state; } private static int lastContactId = 0; public static ArrayList<Contact> createContactsList(int numContacts) { ArrayList<Contact> contacts = new ArrayList<>(); for (int i = 1; i <= numContacts; i++) { contacts.add(new Contact("person " + ++lastContactId, true)); } return contacts; } } |
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_main" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_margin="10dp" android:weightSum="10" android:background="#B9B8B8"> <TextView android:id="@+id/rv_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="9" android:text="test text" android:textColor="#000" android:textSize="24sp" /> <CheckBox android:id="@+id/rv_chk" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" /> </LinearLayout> |