MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Goods> mGoodsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGoodsList();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
ListAdapter adapter = new ListAdapter(mGoodsList);
recyclerView.setAdapter(adapter);
}
private void initGoodsList() {
for (int i = 0; i < 3; i++) {
Goods table = new Goods(R.drawable.table, "桌子", "100元");
Goods apple = new Goods(R.drawable.apple, "苹果", "10元/斤");
Goods cake = new Goods(R.drawable.cake, "蛋糕", "108元");
Goods sweater = new Goods(R.drawable.sweater, "线衣", "120元");
mGoodsList.add(table);
mGoodsList.add(apple);
mGoodsList.add(cake);
mGoodsList.add(sweater);
}
}
}
ListAdapter.java
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private List<Goods> mGoodsList;
public ListAdapter(List<Goods> goodsList) {
mGoodsList = goodsList;
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView goodsImage;
TextView goodsName;
TextView goodsPrice;
public ViewHolder(@NonNull View itemView) {
super(itemView);
goodsImage = itemView.findViewById(R.id.image);
goodsName = itemView.findViewById(R.id.name);
goodsPrice = itemView.findViewById(R.id.price);
}
}
//用于创建ViewHolder实例
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
//用于对 RecyclerView子项的数据进行赋值
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Goods goods = mGoodsList.get(position);
holder.goodsImage.setImageResource(goods.getImgId());
holder.goodsName.setText(goods.getName());
holder.goodsPrice.setText(goods.getPrice());
}
@Override
public int getItemCount() {
return mGoodsList.size();
}
}
Goods.java
package com.example.recycle;
public class Goods {
private Integer imgId;
private String name;
private String price;
public Goods(Integer imgId, String name, String price) {
this.imgId = imgId;
this.name = name;
this.price = price;
}
public Integer getImgId() {
return imgId;
}
public void setImgId(Integer imgId) {
this.imgId = imgId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>