RecyclerView是ListView的加強版,一樣的是他們的有 Adapter 來呈現 handle item view的內容。不一樣的是它比 ListView 更彈性。
第一步:添加需要使用的套件到build.dradle
implementation 'com.android.support:recyclerview-v7:30.4.0'
第二步:在布局文件放入 RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/recyclerview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
第三步:製作 RecyclerView 的樣式 (item.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="178dp"
android:layout_height="38dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView2"
android:layout_width="213dp"
android:layout_height="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
第四步:在主程式呼叫需要使用的元件以及 RecyclerView 的基本語法
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<String> grop=new ArrayList<>();
List<String> customer=new ArrayList<>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for(int i=1;i<4;i++){
String number =String.valueOf(i);
String many =String.valueOf(i+3);
grop.add(number);
customer.add(many);
recyclerView.setAdapter(new MyAdapter(grop,customer));
}
}
}
第五步:製作 RecyclerView 的 adapter
package com.example.testr;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH> {
public static class VH extends RecyclerView.ViewHolder{
//此段放當傳入值時要顯示在 item 上的元件
public TextView textView1,textView2;
public VH(View v) {
super(v);
textView1 = v.findViewById(R.id.textView1);
textView2 = v.findViewById(R.id.textView2);
}
}
//此段為在主程式接收的資料
private List<String> mgrop;
private List<String> mcostomer;
public MyAdapter(List<String> grop,List<String> costomer) {
this.mgrop = grop;
this.mcostomer = costomer;
}
@NonNull
@Override
public MyAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new VH(v);
}
@Override
//此段為顯示在item
public void onBindViewHolder(@NonNull MyAdapter.VH holder, int position) {
holder.textView1.setText(mgrop.get(position));
holder.textView2.setText(mcostomer.get(position));
}
@Override
//此段為知道有幾筆資料
public int getItemCount() {
return mgrop.size();
}
}
**注意!!!
**如只顯示一筆請將 RecyclerView 的 item 的高設為 wrap_content