RecycleView

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值