- 刚接触安卓
- 记录一下Recycler View的思路
- 比较基础
1.引入依赖
implementation 'androidx.recyclerview:recyclerview:1.2.1'
2.绘制布局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="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
3.Java代码
- 以下是Java代码的写法
- 首先set了LayoutManager
- 之后创建了一个Adapter
- 再之后set了Adapter
- 再ViewHolder的构造函数中对项进行监听
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Person> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
initData();
RecyclerView.Adapter adapter = new RecyclerView.Adapter<PersonViewHolder>() {
@NonNull
@Override
public PersonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
return new PersonViewHolder(view, this);
}
@Override
public void onBindViewHolder(@NonNull PersonViewHolder holder, int position) {
holder.nameTv.setText(list.get(position).getName());
holder.descTv.setText(list.get(position).getDesc());
holder.headerTv.setImageResource(list.get(position).getImage());
}
@Override
public int getItemCount() {
return list.size();
}
};
recyclerView.setAdapter(adapter);
}
private void initData() {
String[] names = new String[]{"wang", "hua", "liu", "deng"};
String[] desc = new String[]{"ziqi", "jiahua", "xuxin", "wenwei"};
int[] imageIds = new int[]{R.drawable.ic_baseline_emoji_emotions_24,
R.drawable.ic_baseline_emoji_emotions_24,
R.drawable.ic_baseline_emoji_emotions_24,
R.drawable.ic_baseline_emoji_emotions_24};
for (int i = 0; i < names.length; i++) {
list.add(new Person(names[i], desc[i], imageIds[i]));
}
}
class PersonViewHolder extends RecyclerView.ViewHolder {
View rootView;
TextView nameTv;
TextView descTv;
ImageView headerTv;
private RecyclerView.Adapter adapter;
public PersonViewHolder(@NonNull View view, RecyclerView.Adapter adapter) {
super(view);
this.nameTv = view.findViewById(R.id.name);
this.descTv = view.findViewById(R.id.desc);
this.headerTv = view.findViewById(R.id.header);
this.adapter = adapter;
this.rootView = view.findViewById(R.id.root);
rootView.setOnClickListener(view1 -> {
TextView nametv = view1.findViewById(R.id.name);
Toast.makeText(MainActivity.this, "您点击了:" + nametv.getText()
, Toast.LENGTH_SHORT).show();
});
}
}
}
class Person {
private String name;
private String desc;
private int image;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public Person(String name, String desc, int image) {
this.name = name;
this.desc = desc;
this.image = image;
}
}
最终效果
- 这个图和上面代码的数据用的不一样,但是效果是一样的