移动互联网开发作业之recycleview设计

移动互联网开发作业之recycleview设计

项目要求

在原有作业1(见上一篇博文)上选择一个tab添加recycleview显示。显示内容不做限定,可以是新闻、商品、球队、明星、快递时间轴、漫画等。

项目源码地址

码云仓库地址:https://gitee.com/si–yue/MyWeChatDemo/tree/master/

recycleview设计

  1. 向tab01界面中拖进RecyclerView控件,创建依赖并且添加布局。
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"/>
  1. 创建一个item.xml文件,为RecyclerView中的元素设定样式
<?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:background="#ffffff">

    <LinearLayout
    android:id="@+id/ll_1"
    android:layout_width="wrap_content"
    android:layout_height="125dp"
    android:gravity="center">
    <ImageView
        android:id="@+id/item_goods_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@mipmap/picture1"
        android:background="#4D2BD5"/>
</LinearLayout>

    <LinearLayout
        android:id="@+id/ll_2"
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/ll_2_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp">
            <TextView
                android:id="@+id/item_goods_nametitle"
                android:layout_width="90dp"
                android:layout_height="25dp"
                android:text=""
                android:textSize="16sp"
                android:textColor="#000000" />
            <TextView
                android:id="@+id/item_goods_name"
                android:layout_width="190dp"
                android:layout_height="25dp"
                android:text=""
                android:textSize="20sp"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_2_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
            <TextView
                android:id="@+id/item_goods_pricetitle"
                android:layout_width="90dp"
                android:layout_height="25dp"
                android:text=""
                android:textSize="16sp"
                android:textColor="#000000" />
            <TextView
                android:id="@+id/item_goods_price"
                android:layout_width="190dp"
                android:layout_height="25dp"
                android:text=""
                android:textSize="20sp"
                android:textColor="#000000" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
  1. 创建适配器adapter
class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.myViewHolder> {

    private OnItemClickListener onItemClickListener;
    private Context context;
    private ArrayList<GoodsEntity> goodsEntities;

    public LinearAdapter(Context context,ArrayList<GoodsEntity> goodsEntities){
        this.context = context;
        this.goodsEntities = goodsEntities;
    }

    @NonNull
    @Override
    public LinearAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.layout_linear_item,null);
        return new myViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull LinearAdapter.myViewHolder holder, int position) {
        GoodsEntity data = goodsEntities.get(position);
        holder.mItemGoodsName.setText(data.goodsName);
        holder.mItemGoodsPrice.setText(data.goodsPrice);
        holder.mItemGoodsNameTitle.setText(data.goodsNameTitle);
        holder.mItemGoodsPriceTitle.setText(data.goodsPriceTitle);
    }

    @Override
    public int getItemCount() {
        return goodsEntities.size();
    }

    class myViewHolder extends RecyclerView.ViewHolder {

        private ImageView mItemGoodsImg;
        private TextView mItemGoodsName;
        private TextView mItemGoodsPrice;
        private TextView mItemGoodsNameTitle;
        private TextView mItemGoodsPriceTitle;

        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            mItemGoodsImg = itemView.findViewById(R.id.item_goods_img);
            mItemGoodsName = itemView.findViewById(R.id.item_goods_name);
            mItemGoodsPrice = itemView.findViewById(R.id.item_goods_price);
            mItemGoodsNameTitle = itemView.findViewById(R.id.item_goods_nametitle);
            mItemGoodsPriceTitle = itemView.findViewById(R.id.item_goods_pricetitle);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (onItemClickListener!=null){

                        onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
                    }
                }
            });

        }
    }

    public interface OnItemClickListener {
        public void OnItemClick(View view, GoodsEntity data);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
  1. 创建一个java文件,编写获取页面信息的函数(获取文字和图片信息)
class GoodsEntity implements Serializable {
    public String imgPath;
    public String goodsName;
    public String goodsPrice;
    public String goodsNameTitle;
    public String goodsPriceTitle;

    public GoodsEntity() {
    }

    public GoodsEntity(String imgPath, String goodsName, String goodsPrice,String goodsNameTitle,String goodsPriceTitle) {
        this.imgPath = imgPath;
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
        this.goodsNameTitle = goodsNameTitle;
        this.goodsPriceTitle = goodsPriceTitle;

    }
    
    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }


    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }


    public String getGoodsNameTitle(){
        return goodsNameTitle;
    }

    public void setGoodsNameTitle(String goodsNameTitle){
        this.goodsNameTitle = goodsNameTitle;
    }

    public  String getGoodsPriceTitle(){
        return goodsPriceTitle;
    }

    public void setGoodsPriceTitle(String goodsPriceTitle){
        this.goodsPriceTitle = goodsPriceTitle;
    }


    public String getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(String goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    @Override
    public String toString() {
        return "GoodsEntity{" +
                "imgPath='" + imgPath + '\'' +
                ", goodsName='" + goodsName + '\'' +
                ", goodsNameTitle='" + goodsNameTitle + '\'' +
                ", goodsPrice='" + goodsPrice + '\'' +
                ", goodsPriceTitle='" + goodsPriceTitle + '\'' +
                '}';
    }

}

  1. 在MainActivity.java文件中获取RecyclerView对象,调用获取图片和文字信息的函数,实例化适配器以及设置LayoutManager和Adapter。

参考网址:

1、https://www.jianshu.com/p/c4cfe38a91ed
2、https://developer.android.google.cn/guide/topics/ui/layout/recyclerview#java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RecycleView 是一个用于在 Android 应用中展示大量数据的高效视图组件。它可以用于显示列表、网格或瀑布流等不同类型的布局,并支持高度的重用和回收。这个组件可以大大提高应用程序的性能,因为它只会在屏幕上显示可见项,而不是将所有数据一次性加载到内存中。 使用 RecycleView,您需要创建一个适配器(Adapter)来将数据绑定到视图上,并且可以自定义视图的外观和交互。您可以使用默认的适配器(如 ArrayAdapter)或自定义适配器来满足特定的需求。 以下是一个示例代码,演示如何使用 RecycleView 在一个简单的列表中显示一组文本项: ```java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> mData; public MyAdapter(List<String> data) { mData = data; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { String item = mData.get(position); holder.textView.setText(item); } @Override public int getItemCount() { return mData.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView textView; public ViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.text_view); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值