Android 9 Development Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

Creating RecyclerView is as simple as placing the control on the screen.  Most of the work is with the adapter, which we'll create from a static list. RecyclerView is distributed in a separate library so it needs to be added to the project as a dependency.  The steps are as follows: 

  1. Either add the dependency through the Android Studio UI or add the following code to the dependencies section of the build.gradle (Module: app) file:
    implementation 'com.android.support:recyclerview-v7:27.1
NOTE: v7:27.1 is current at the time of this writing, but should be updated to the latest version.  (The IDE will likely give you a warning if you're not using the latest version.)
  1.  Open activity_main.xml and replace the existing <TextView /> block with the following RecyclerView widget:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
  1. We need another layout for the adapter to create the individual items in the list. To do this, create a new file in the res\layout folder called item.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
  1. Now comes the heart of RecyclerView – the adapter. Create a new Java file called MyAdapter.java. Our new class will extend from the RecylerView.Adapter class so there are several key methods we need to override. We’ll discuss the details of this class later, but the full code is as follows:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private List<String> nameList;

public MyAdapter(List<String> list) {
nameList = list;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
final String name = nameList.get(position);
holder.textView.setText(name);
}

@Override
public int getItemCount() {
if (nameList==null) {
return 0;
} else {
return nameList.size();
}
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;

public MyViewHolder(View itemVieww) {
super(itemVieww);
textView = itemView.findViewById(R.id.textView);
}
}
}
  1. With all the pieces set up, the final step is to put it all together. Open the MainActivity.java file and add the following code to the existing onCreate() method:
List<String> list = new ArrayList<>();
list.add("China");
list.add("France");
list.add("Germany");
list.add("India");
list.add("Russia");
list.add("United Kingdom");
list.add("United States");

RecyclerView recyclerView = findViewById(R.id.recyclerView);

recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

MyAdapter myAdapter = new MyAdapter(list);
recyclerView.setAdapter(myAdapter);