RecyclerView 简单使用方法

1、依赖:

    //添加RecyclerView的依赖包
    implementation 'androidx.recyclerview:recyclerview:1.2.1'

2、xml布局:

 
        
        
        

    

3、Adapter代码:

package com.chy.demoprj.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chy.demoprj.R;

import java.util.HashMap;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public class MyRecyclerAdapter extends RecyclerView.Adapter {

    private OnItemClickListener itemClickListener;// 接口对象
    private Context context;// 上下文
    private List> itemList;// 数据集合
    private int lastIndex = -1;

    public MyRecyclerAdapter(Context context,List> itemList){
        super();
        this.context = context;
        this.itemList = itemList;
        lastIndex = itemList.size()-1;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        // TODO 自动生成的方法存根
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list_point_cell,null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;

    }


    @Override
    public void onBindViewHolder(MyViewHolder holder,int position) {
        // 设置未选中状态
        holder.item_point_img.setImageDrawable(context.getDrawable(R.mipmap.ic_unselected_circle));
        holder.item_start_img.setVisibility(View.VISIBLE);
        holder.item_end_img.setVisibility(View.VISIBLE);
        // 隐藏第一个
        if (position==0){
            holder.item_start_img.setVisibility(View.INVISIBLE);
        }
        // 隐藏最后一个
        if (position == lastIndex){
            holder.item_end_img.setVisibility(View.INVISIBLE);
        }

        SharedPreferences time_sp = context.getSharedPreferences("TIME_LINE_SP",MODE_PRIVATE);
        int index = time_sp.getInt("INDEX_TIME",-1);
        if (index==position){
            // 设置选中状态
            holder.item_point_img.setImageDrawable(context.getDrawable(R.mipmap.ic_selected_circle));
        }

        HashMap hashMap = itemList.get(position);
        // 赋值
        holder.time.setText(hashMap.get("TIME"));
        holder.tv.setText(hashMap.get("TEXT"));


    }

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


  protected class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView time;
        private ImageView item_start_img;
        private ImageView item_point_img;
        private ImageView item_end_img;
        private TextView tv;
        /**
         * @param itemView
         */
        public MyViewHolder(View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.item_time);
            item_start_img = itemView.findViewById(R.id.item_start_img);
            item_point_img = itemView.findViewById(R.id.item_point_img);
            item_end_img = itemView.findViewById(R.id.item_end_img);
            tv = itemView.findViewById(R.id.item_text);

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClickListener.onItemClick(getAbsoluteAdapterPosition());
                }
            });
        }
    }


    /**
     * 点击接口方法(监听)
     * */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    /**
     * 回调函数
     * */
    public void ItemClickCallBack(OnItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

}

4、使用:

      // 初始化显示的数据
        initData();
        rv_one = findViewById(R.id.rv_one);

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        // 竖向布局
        //linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        // 横向布局
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        // 设置布局
        rv_one.setLayoutManager(linearLayoutManager);

        // 设置adpater
        MyRecyclerAdapter adapter = new MyRecyclerAdapter(this,listItem);
        adapter.ItemClickCallBack(new MyRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                SharedPreferences time_sp = getSharedPreferences("TIME_LINE_SP",MODE_PRIVATE);
                SharedPreferences.Editor editor = time_sp.edit();
                editor.putInt("INDEX_TIME",position);
                editor.commit();
                // 刷新
                adapter.notifyDataSetChanged();
            }
        });
        rv_one.setAdapter(adapter);

5、数据初始化

 /*在数组中存放数据*/
        listItem = new ArrayList>();

        HashMap map1 = new HashMap(20);
        HashMap map2 = new HashMap(20);
        HashMap map3 = new HashMap(20);
        HashMap map4 = new HashMap(20);
        HashMap map5 = new HashMap(20);
        HashMap map6 = new HashMap(20);

        map1.put("TIME", "2023-8-3");
        map1.put("TEXT", "发件人:谷歌 CEO");
        listItem.add(map1);

        map2.put("TIME", "2023-8-27");
        map2.put("TEXT", "等待中转");
        listItem.add(map2);

        map3.put("TIME", "2023-9-4");
        map3.put("TEXT", "下一站中国");
        listItem.add(map3);

        map4.put("TIME", "2023-9-20");
        map4.put("TEXT", "下一站深圳龙岗区代理");
        listItem.add(map4);

        map5.put("TIME", "2023-10-9");
        map5.put("TEXT", "等待派件");
        listItem.add(map5);

        map6.put("TIME", "2023-11-10");
        map6.put("TEXT", "收件人:你的名字");
        listItem.add(map6);

你可能感兴趣的:(android)