[JAVA] Now, I've summarized the basics of RecyclerView

What is RecyclerView is?

  • A widget that dynamically repeats the View

  • Included in Support Library (com.android.support: recyclerview-v7)

Example 1.png

How to use?

  • To use RecyclerView, you need to create Adapter and ViewHolder at a minimum.
  • Use LayoutManager for layout management of RecyclerView

Let's make it

Constitution

  • This time I will make it with the minimum configuration
  • RecyclerViewActivity.java ・ ・ ・ Display RecyclerView
  • RecyclerViewAdapter.java ・ ・ ・ Associate View with data
  • activity_recycler_view.xml ・ ・ ・ Layout of the entire RecyclerView
  • row.xml ・ ・ ・ Layout of one row in RecyclerView

procedure

(1) Describe the dependency of Support Library in build.gradle

_ (CardView is also used this time, so describe it) _

build.gradle


dependencies {
	...
    compile 'com.android.support:recyclerview-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
  
    ...
}

(2) Define the layout of the entire RecyclerView

activity_recycler_view.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

(3) Define the layout for each line

row.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:layout_margin="8dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp" />
</android.support.v7.widget.CardView>

(4) Describe the initialization process of RecyclerView with Activity

RecyclerViewActivity.java



public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerViewAdapter recyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

		 // [1] 
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

		 // [2] 
        recyclerViewAdapter = new RecyclerViewAdapter(getListData());
        recyclerView.setAdapter(recyclerViewAdapter);
    }

    private List<String> getListData() {
        ArrayList<String> list = new ArrayList<>();
        list.add("January");
        list.add("February");
        list.add("Yayoi");
        list.add("Uzuki");
        list.add("Satsuki");
        list.add("Minazuki");
        list.add("July");
        list.add("Hazuki");
        list.add("Nagatsuki");
        list.add("Kannazuki");
        list.add("Shimotsuki");
        list.add("December");
        return list;
    }

}

(5) Create Adapter class and link data and View

RecyclerViewAdapter.java



public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    protected List<String> dataSet;

	// [3]
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView textView;

        public ViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.text_view);
        }
    }
    
	// [4]
    public RecyclerViewAdapter(List<String> myDataSet) {
        dataSet = myDataSet;
    }

	// [5]
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

	// [6]
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String text = dataSet.get(position);
        holder.textView.setText(text);
    }

	// [7]
    @Override
    public int getItemCount() {
        return dataSet.size();
    }

}


Description

  • [1] An instance of LinearLayoutManager is created and passed as an argument of the setLayoutManager () method of RecyclerView. In addition to LinearLayoutManager, there are other types such as GridLayoutManager. Change according to the layout you want to display.

  • [2] An Adapter is generated and set in the view. This time, the data is also passed at the timing of generating the Adapter.

  • [3] A class that inherits from RecyclerView.ViewHolder. Holds a reference to View. The onCreateViewHolder () method in [5] returns an instance of ViewHolder.

  • [4] This time, the data is also passed when the Adapter is generated.

  • [5] View inflate (inflate), ViewHolder are created and returned.

  • [6] Data is set for ViewHolder.

  • [7] Returns the number of items to display in RecyclerView.

Impressions

  • I was able to follow the process flow, but to be honest, I have a feeling that I haven't grasped what LayoutManager is doing behind the scenes and what ViewHolder is doing.

Recommended Posts