Android之RecyclerView(新手模板)

1,RecyclerView的简单使用

我的是androidX,不是v7,v7的依赖和下面这个不一样,其他基本相同

1,添加依赖

 implementation 'androidx.recyclerview:recyclerview:1.0.0'

2,添加列表控件
这个就是列表,添加到布局文件相应的地方

 <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

3,创建list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_margin="4dp"
    android:background="@drawable/table_style"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="90dp">

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/ll_item"
        android:layout_width="match_parent"
        android:layout_height="70dp">
        <TextView
            android:id="@+id/tv_list_id"
            android:layout_width="1dp"
            android:layout_height="1dp" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/iv_list_img"
                android:layout_gravity="center"
                android:src="@drawable/img_in"
                android:layout_weight="2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <TextView
                android:id="@+id/tv_list_money"
                android:text="100"
                android:textSize="20sp"
                android:gravity="center"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tv_list_remarks"
                android:text="备注"
                android:textSize="20sp"
                android:gravity="left"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <LinearLayout
                android:layout_weight="2"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/tv_list_label"
                    android:text="标签"
                    android:textSize="14sp"
                    android:gravity="left|center"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                <TextView
                    android:id="@+id/tv_list_time"
                    android:text="时间"
                    android:textSize="14sp"
                    android:gravity="left|center"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

4,创建一个类ListItem,这个类里面的都是每个list_item的信息

public class ListItem  {
    int id;
    private Integer img;
    private String sum;
    private String remarks;
    private String label;
    private String time;


    public ListItem(int id, Integer img, String sum, String remarks, String label, String time) {
        this.id = id;
        this.img = img;
        this.sum = sum;
        this.remarks = remarks;
        this.label = label;
        this.time = time;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Integer getImg() {
        return img;
    }

    public void setImg(Integer img) {
        this.img = img;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getSum() {
        return sum;
    }

    public void setSum(String sum) {
        this.sum = sum;
    }
}

3,创建一个ListAdapter适配

//RecyclerView.Adapter<MyListAdapter.ViewHolder>
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {

    List<ListItem> list;

    public ListAdapter(List<ListItem> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent,false);
        ViewHolder viewHolder =new ViewHolder(view);

        //点击事件
        view.findViewById(R.id.ll_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv_id = v.findViewById(R.id.tv_list_id);
                int id = Integer.valueOf(tv_id.getText().toString());
                Intent intent = new Intent(parent.getContext(), EditAccount.class);
                intent.putExtra("id", id);
                parent.getContext().startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ListItem p = list.get(position);          //获取第pos个信息

        if(Integer.valueOf(p.getSum())>0){
            holder.img.setImageResource(R.drawable.img_in);
        }else{
            holder.img.setImageResource(R.drawable.img_out);
        }
        //赋值
        holder.sum.setText("¥"+p.getSum());
        holder.remarks.setText("备注:"+p.getRemarks());
        holder.label.setText("标签:"+p.getLabel());
        holder.time.setText("时间:"+p.getTime());
        holder.id.setText(p.getId()+"");
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView img;
        TextView sum;
        TextView remarks;
        TextView label;
        TextView time;
        TextView id;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            //list_item里面的ID
            img = itemView.findViewById(R.id.iv_list_img);
            sum = itemView.findViewById(R.id.tv_list_money);
            remarks = itemView.findViewById(R.id.tv_list_remarks);
            label = itemView.findViewById(R.id.tv_list_label);
            time = itemView.findViewById(R.id.tv_list_time);
            id = itemView.findViewById(R.id.tv_list_id);
        }
    }
}

6,开始使用

//全局变量
ListAdapter listAdapter;
List<ListItem> list;
 
 //onCreate里面调用
list = new ArrayList<>();
initDatas();     //初始化数据
initRecycleView();      //加载RecycleView

private void initDatas() {           //初始化数据
       List<ListItem> list_all = MyData.queryAll();
       //把数据放到list_all
       
       list.clear();
       list.addAll(list_all); //不能用等于号
}
private void initRecycleView() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        listAdapter = new ListAdapter(list);
        recyclerView.setAdapter(listAdapter);
}



  //列表更新,由看不见变为可见是调用
    @Override
    protected void onResume() {
        super.onResume();
        initDatas();
        listAdapter.notifyDataSetChanged();        //更新说明
    }

发布了15 篇原创文章 · 获赞 15 · 访问量 1167

猜你喜欢

转载自blog.csdn.net/weixin_44826485/article/details/104661180