在app-Fragment中运用ReclerView

app-Fragment中运用ReclerView

仓库地址 https://gitee.com/yagao/codes/u74yjnts6zo5rwevgb0xp79

在Fragment控件中增加ReclerView控件,增添首页内容。

步骤

  1. 在fragment对应的layout中添加ReclerView控件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"/>

</LinearLayout>

2.对控件的函数控制.

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. 创建ReclerView的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. weixinFragment:
public class weixinFragment extends Fragment {

    private RecyclerView mRvMain; //定义ReclerView控件
    private View view;//定义view来设置fragment中的layout
    private ArrayList<GoodsEntity> goodsEntities = new ArrayList<GoodsEntity>();
    private LinearAdapter mLinearAdapter;


    public weixinFragment() {

        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tab01, container, false);

        initRecyclerView();

        initData();

        return view;

    }

    //
    private void initRecyclerView() {
        mRvMain = (RecyclerView)view.findViewById(R.id.rv_main);
        mLinearAdapter = new LinearAdapter(getActivity(),goodsEntities);
        mRvMain.setAdapter(mLinearAdapter);
        mRvMain.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));

        mRvMain.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));

        mLinearAdapter.setOnItemClickListener(new LinearAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, GoodsEntity data) {
                Toast.makeText(getActivity(),"图片售罄",Toast.LENGTH_SHORT).show();
            }
        });




    }

    //
    private void initData(){
        for (int i=0;i<10;i++){
            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setGoodsNameTitle("  图片名称:");
            goodsEntity.setGoodsName("图片序号"+i);
            goodsEntity.setGoodsPriceTitle("  图片价格:");
            goodsEntity.setGoodsPrice("1"+i*100+"RMB");
            goodsEntities.add(goodsEntity);

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值