A widget that dynamically repeats the View
Included in Support Library (com.android.support: recyclerview-v7)
Example
_ (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'
...
}
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" />
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>
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;
}
}
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();
}
}
[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.
Recommended Posts