<?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="horizontal"
android:weightSum="3">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tv_name"
android:layout_weight="1"
android:layout_gravity="center"
android:text="千手柱間" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tv_number"
android:layout_weight="1"
android:layout_gravity="center"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tv_trump"
android:layout_weight="1"
android:layout_gravity="center"
android:text="木遁" />
</LinearLayout>
activity_main.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="wrap_content"
android:layout_height="0dp"
android:id="@+id/recycle_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Huoying.java package com.example.recycleviewdemo;
public class Huoying
{
private int number;
private String name;
private String trump;
public Huoying(int number, String name, String trump)
{
this.number = number;
this.name = name;
this.trump = trump;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTrump()
{
return trump;
}
public void setTrump(String trump)
{
this.trump = trump;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
@Override
public String toString()
{
return "Huoying{" +
"number=" + number +
", name='" + name + '\'' +
", trump='" + trump + '\'' +
'}';
}
}
HuoYingAdapter.java package com.example.recycleviewdemo;
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 HuoYingAdapter extends RecyclerView.Adapter<HuoYingAdapter.HuoYingViewHolder>{
private List<Huoying> huoyingList;
public HuoYingAdapter(List<Huoying> huoyingList){
this.huoyingList=huoyingList;
}
@NonNull
@Override
public HuoYingAdapter.HuoYingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
return new HuoYingViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HuoYingAdapter.HuoYingViewHolder holder, int position) {
holder.tv_name.setText(huoyingList.get(position).getName());
holder.tv_number.setText(String.valueOf(huoyingList.get(position).getNumber()));
holder.tv_trump.setText(huoyingList.get(position).getTrump());
}
@Override
public int getItemCount() {
return huoyingList.size();
}
public static class HuoYingViewHolder extends RecyclerView.ViewHolder{
TextView tv_name,tv_number,tv_trump;
public HuoYingViewHolder(View view){
super(view);
tv_name=view.findViewById(R.id.tv_name);
tv_number=view.findViewById(R.id.tv_number);
tv_trump=view.findViewById(R.id.tv_trump);
}
}
}
MainActivity package com.example.recycleviewdemo;
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.List;
public class MainActivity extends AppCompatActivity {
private List<Huoying> huoyingList;
private RecyclerView recyclerView;
private HuoYingAdapter huoYingAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
huoyingList = new ArrayList<>(7);
huoyingList.add(new Huoying(1, "千手柱間", "木遁"));
huoyingList.add(new Huoying(2, "千手扉間", "水遁"));
huoyingList.add(new Huoying(3, "猿飛日斬", "猿魔"));
huoyingList.add(new Huoying(4, "波風水門", "飛雷神"));
huoyingList.add(new Huoying(5, "千手綱手", "百豪之術"));
huoyingList.add(new Huoying(6, "旗木卡卡西", "雷切"));
huoyingList.add(new Huoying(7, "漩渦鳴人", "螺旋丸"));
recyclerView=findViewById(R.id.recycle_view);
huoYingAdapter=new HuoYingAdapter(huoyingList);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(huoYingAdapter);
}
}
源碼:github
上述程式可能遇到的一個錯誤及解決方法:[android.content.res.Resources$NotFoundException: String resource ID #0x1](https://blog.csdn.net/jsfjsf1/article/details/49130971)