Step 1: Add a statement to the dependencies closure of the app/build.gradle file
compile 'com.android.support:recyclerview-v7:24.2.1'
Remember to click Sync Now in the upper right corner to synchronize
Step 2: Add the code of the RecyclerView control in activity_main.xml to create a sub-item class and create a layout file for the sub-item
Example:
<android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
The third step is to create an adapter class and create a ViewHolder class in the class to introduce the layout of children in this class
and specify the adapter class generic as <NewsAdapter.ViewHolder>
class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> { private List<News> mNewsList; class ViewHolder extends RecyclerView.ViewHolder { TextView newsTitleText; public ViewHolder(View view) { //Introduce the child item layout file super(view); newsTitleText = (TextView) view.findViewById(R.id.news_title); } } public NewsAdapter(List<News> newsList) { //Get child item data mNewsList = newsList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //Create corresponding child instances View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { //Assign the sub-item data News news = mNewsList.get(position); holder.newsTitleText.setText(news.getTitle()); } @Override public int getItemCount() { //return the number of child items return mNewsList.size(); } }