zoukankan      html  css  js  c++  java
  • 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();
        }
    }

    就搞定啦!

  • 相关阅读:
    每日踩坑 2018-01-09 WebAPI会如何面对URL中的空串string参数?
    每日踩坑 2018-12-25 【Unable to convert MySQL date/time value to System.DateTime】异常
    面向对象设计原则 单一职责原则(Single responsibility principle)
    C# EF Attach 与 Entry
    每日踩坑 2018-11-26 MVC Razor ActionLink 生成的URL中多生成了一个参数 ?length=n
    工具 在 Nuget 发布自己的包
    LoadRunner中 host-mapping的Capture Level说明
    使用loadrunner录制脚本的思路和注意要点
    LR两种录制模式的区别
    LoadRunner脚本参数化之自动关联和手动关联
  • 原文地址:https://www.cnblogs.com/Nora-F/p/10288546.html
Copyright © 2011-2022 走看看