• RecycleView 的使用 (CardView显示每个小项)


    转自 https://www.jianshu.com/p/bb6b029de04f

    先说布局:

    主页面:

     <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#d4d4d4" />

    每个小项的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:id="@+id/cv_item"
        card_view:cardCornerRadius="4dp">
    
        <TextView
            android:id="@+id/calendar_item_tv_Cusname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="上海有限公司"
            android:textSize="20sp" />
    </android.support.v7.widget.CardView>

    代码部分:

    主页面 onCreate:

    mRecyclerView =   findViewById(R.id.recyclerView);
            myAdapter = new MyRecyclerViewAdapter(this,list);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setAdapter(myAdapter);

    在每次刷新的时候

    list.clear();
    list.add(c);
    myAdapter.Update(list);

    MyRecyclerViewAdapter 类
    public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyHolder> {
        Context context;
        List<CusPlan> list;
    
        public MyRecyclerViewAdapter(Context context, List<CusPlan> list) {
            this.context = context;
            this.list = list;
        }
    
        public void Update(List<CusPlan> li){
            this.list = li;
            notifyDataSetChanged();
        }
    
        class MyHolder extends RecyclerView.ViewHolder{
      //布局里面只有一个TextView显示客户名称
            TextView tv_cusName;
            public MyHolder(@NonNull View itemView) {
                super(itemView);
                tv_cusName= itemView.findViewById(R.id.calendar_item_tv_Cusname);
            }
        }
    
        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view =LayoutInflater.from(viewGroup.getContext()).inflate(
                    R.layout.calendar_event_item,viewGroup,false);  //单个小项的布局名字
            MyHolder myHolder = new MyHolder(view);
            return myHolder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int pos) {
            CusPlan s = list.get(pos);
            myHolder.tv_cusName.setText(s.getCusName());
            //对控件侦听
            myHolder.tv_cusName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                }
            });
    
            //对整个item侦听
            myHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    就搞定啦!

  • 相关阅读:
    焦虑来回走
    去省政府客串
    中国地质大学(北京)招生信息有点坑
    张桂梅校长再获殊荣,实至名归!她的故事值得一看再看……
    行内容转为列内容
    公文写作心得
    钟南山院士亲口说的“如何保持健康长寿
    VMware虚拟机出现“内部错误”如何解决?
    CI框架深入篇(2)一些基础的我之不知道的标准格式
    SQL语句学习记录(三)
  • 原文地址:https://www.cnblogs.com/Nora-F/p/10288546.html
Copyright © 2020-2023  润新知