大家好,今天我要分享RecyclerView的使用方法,那我們就開始吧。
在activity_main.xml新增RecyclerView元件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
recyclerview_textview.xml,RecyclerView的列表內容的外觀都會依照此xml顯示
<?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/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="TextView"
android:textSize="30sp"/>
</LinearLayout>
新增在RecyclerView裡呈現的內容
String[] cal ={"January","February","March","April","May","June","July","August",
"September","October","November","December"};
private ArrayList<String> mitem=new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerV();
for (int i=0; i < cal.length; i++){
mitem.add(cal[i]);
}
}
完整程式碼
package com.example.test;
import ...
public class MainActivity extends AppCompatActivity {
RecyclerAdapter adapter;
String[] cal ={"January","February","March","April","May","June","July","August",
"September","October","November","December"};
private ArrayList<String> mitem=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerV();
for (int i=0; i < cal.length; i++){
mitem.add(cal[i]);
}
}
//設置RecyclerView
private void RecyclerV() {
RecyclerView recycle =findViewById(R.id.recycle);
recycle.setLayoutManager(new LinearLayoutManager(this));
recycle.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
adapter=new RecyclerAdapter(this,mitem);
recycle.setAdapter(adapter);
}
}
package com.example.test;
import ...
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<String> mitem;
private LayoutInflater inflater;
RecyclerAdapter(Context context,List<String> item){
this.inflater=LayoutInflater.from(context);
this.mitem=item;
}
//取得控制元件
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
}
}
//連接recyclerview_textview.xml
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view =inflater.inflate(R.layout.recyclerview_textview,parent,false);
return new ViewHolder(view);
}
//連結剛才在ViewHolder宣告過的元件,就可以從mitem中取出你要的值
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.title.setText(mitem.get(position));
}
//取得顯示數量的大小,return 陣列長度
@Override
public int getItemCount() {
return mitem.size();
}
}
成果圖