RecyclerView是可以滑動的的列表,能自己設計資料顯示的版面長怎樣,當資料數量過多超出RecyclerView的範圍時會變成可以滑動的狀態
RecyclerView為內建的元件所以直接拉進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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="409dp"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
這裡是用來設計每一筆資料顯示時的版面
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textSize="25dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textSize="25dp" />
</LinearLayout>
package com.example.recyclerview_demo;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
//接收在activity的arrayList的假資料
public Adapter(ArrayList<HashMap<String, String>> arrayList) {
this.arrayList = arrayList;
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//item布局生成
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
//控制元件的顯示
holder.setData(position);
}
@Override
public int getItemCount() {
//顯示資料的數量
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView ID ,num;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//綁定item.xml裡的元件
ID = itemView.findViewById(R.id.textView);
num = itemView.findViewById(R.id.textView2);
}
//提取假資料的函式
void setData(int position){
ID.setText(arrayList.get(position).get("ID"));
num.setText(arrayList.get(position).get("num"));
}
}
}
package com.example.recyclerview_demo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i=1;i<=50;i++){
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("ID", "序號:" + i);
hashMap.put("num", "數量:" + (new Random().nextInt(80)+20));
arrayList.add(hashMap);
}
//綁定recyclerView元件
recyclerView = findViewById(R.id.recyclerview);
//recyclerView初始化
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//每筆資料間的分隔線(可加可不加)
//recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//將資料丟給adapter處理
adapter = new Adapter(arrayList);
//adapter處理完後再交給recyclerView去顯示
recyclerView.setAdapter(adapter);
}
}
HashMap 在這裡我用來創建假資料
它是以key-vlue的放式來存取
只要把HashMap丟到arrayList這樣就創建好假資料了
最後交給Adapter去處理
for (int i=1;i<=50;i++){
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("ID", "序號:" + i);
hashMap.put("num", "數量:" + (new Random().nextInt(80)+20));
arrayList.add(hashMap);
}