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();
    }
}

就搞定啦!

 

posted on 2019-01-18 16:53  石墨方  阅读(170)  评论(0编辑  收藏  举报

导航