RecyclerView使用

1.添加布局

fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/collect_back"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_1"
        android:background="@color/collect_back"
        />
    <RelativeLayout
        android:id="@+id/add_person_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        android:background="@color/home_background">
        <ImageView
            android:id="@+id/person_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_7"
            android:layout_centerVertical="true"
            android:src="@mipmap/person_logo"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/person_logo"
            android:layout_marginLeft="@dimen/dp_7"
            android:layout_centerVertical="true"
            android:text="@string/control_add_person"
            android:textColor="@color/home_blue"
            android:textSize="@dimen/sp_13"
            />
    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/person_list"
        android:layout_marginTop="@dimen/dp_7"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

 item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_130"
    android:orientation="vertical"
    android:background="@color/home_background">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_7"
        android:background="@color/collect_back">

    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/name"
            android:text="张三"
            android:textSize="@dimen/sp_12"
            android:textColor="@color/info_text"
            android:gravity="left"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="@dimen/dp_14"
            android:layout_marginTop="@dimen/dp_14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/number"
            android:text="415951199001011113"
            android:textSize="@dimen/sp_12"
            android:textColor="@color/info_text"
            android:gravity="left"
            android:layout_toRightOf="@+id/name"
            android:layout_alignTop="@+id/name"
            android:layout_marginLeft="@dimen/dp_14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/command_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/control_command"
            android:textSize="@dimen/sp_12"
            android:textColor="@color/info_text"
            android:layout_below="@+id/name"
            android:layout_alignLeft="@+id/name"
            android:layout_marginTop="@dimen/dp_7"
            />
        <TextView
            android:id="@+id/command"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缉拿"
            android:textSize="@dimen/sp_12"
            android:textColor="@color/info_text"
            android:layout_toRightOf="@+id/command_title"
            android:layout_alignTop="@+id/command_title"
            android:layout_marginLeft="@dimen/dp_7"
            />
        <TextView
            android:id="@+id/time_title"
            android:text="布控时间:"
            android:textSize="@dimen/sp_9"
            android:textColor="@color/control_time"
            android:layout_below="@+id/command_title"
            android:layout_alignLeft="@+id/name"
            android:layout_marginTop="@dimen/dp_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/start_time"
            android:text="1970-01-01 00:00"
            android:textSize="@dimen/sp_9"
            android:textColor="@color/control_time"
            android:layout_alignTop="@+id/time_title"
            android:layout_toRightOf="@+id/time_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/to"
            android:text="至"
            android:textSize="@dimen/sp_9"
            android:textColor="@color/control_time"
            android:layout_toRightOf="@+id/start_time"
            android:layout_alignTop="@+id/start_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/end_time"
            android:text="2018-01-01 00:00"
            android:textSize="@dimen/sp_9"
            android:textColor="@color/control_time"
            android:layout_alignTop="@+id/time_title"
            android:layout_toRightOf="@+id/to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_1"
            android:layout_below="@+id/end_time"
            android:background="@color/collect_back"
            android:layout_marginStart="@dimen/dp_14"
            android:layout_marginEnd="@dimen/dp_14"
            android:layout_marginTop="@dimen/dp_7"
            />
        <Button
            android:id="@+id/delete"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_22"
            android:text="@string/control_delete"
            android:background="@drawable/radius3_grey_gb"
            android:layout_below="@+id/view"
            android:layout_marginTop="@dimen/dp_7"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="@dimen/dp_14"
            />
        <Button
            android:id="@+id/edit"
            android:layout_width="@dimen/dp_45"
            android:layout_height="@dimen/dp_22"
            android:text="@string/control_edit"
            android:background="@drawable/radius3_grey_gb"
            android:layout_below="@+id/view"
            android:layout_marginTop="@dimen/dp_7"
            android:layout_toLeftOf="@id/delete"
            android:layout_marginRight="@dimen/dp_18"
            />
    </RelativeLayout>
</LinearLayout>

2.在fragment中绑定控件->设置布局管理器->绑定adapter

还可以设置分割线recyclerView.addItemDecoration

public class ControlPersonFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<ControlPerson> controlPersonList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_control_person, null);

        initPerson();
        recyclerView = (RecyclerView) view.findViewById(R.id.person_list);
        LinearLayoutManager layoutmanager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutmanager);
        ControlPersonAdapter adapter = new ControlPersonAdapter(controlPersonList);
        recyclerView.setAdapter(adapter);

//        recyclerView.addItemDecoration(new MyItemDecoration(this.getActivity(), MyItemDecoration.HORIZONTAL_LIST));

        return view;
    }

    private void initPerson() {
        ControlPerson controlPerson1 = new ControlPerson();
        controlPerson1.setCommand("缉拿");
        controlPerson1.setEndTime("2018-01-01 00:00");
        controlPerson1.setStartTime("2018-01-01 00:00");
        controlPerson1.setName("张三");
        controlPerson1.setNumber("170726367");
        controlPerson1.setInControl(true);
        controlPersonList.add(controlPerson1);
        ControlPerson controlPerson2 = new ControlPerson();
        controlPerson2.setCommand("缉拿");
        controlPerson2.setEndTime("2018-01-01 00:00");
        controlPerson2.setStartTime("2018-01-01 00:00");
        controlPerson2.setName("张三");
        controlPerson2.setNumber("170726367");
        controlPerson2.setInControl(true);
        controlPersonList.add(controlPerson2);
    }


}

 3.接下来取Adapter进行初始化

①创建内部类ViewHolder,初始化绑定holder中的控件

②保存创建Adapter时候的list数组,作后续提取数据给item用

③OnCreateViewHolder和OnBindViewHolder

 

onCreateViewHolder()方法,负责承载每个子项的布局。它有两个参数,其中一个是 int viewType;这个viewType是属于list里面的对象的一个属性之一,上边的代码没有用到,使用的话再override一个getItemViewType去获取到这个值就行了,获取到其type后可以对这一条的样式啊、是否根目录啊做些处理。

参考:https://blog.csdn.net/csdn_aiyang/article/details/80094302

onBindViewHolder()方法,负责将每个子项holder绑定数据。俩参数分别是RecyclerView.ViewHolder holder, int position;

public class ControlPersonAdapter extends RecyclerView.Adapter<ControlPersonAdapter.ViewHolder> {

    private final List<ControlPerson> controlPerson;
    private Context context;

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView number;
        TextView commandTitle;
        TextView command;
        TextView startTime;
        TextView endTime;
        Button edit,delete;

        public ViewHolder(View view) {
            super(view);
            name = (TextView)view.findViewById(R.id.name);
            number = (TextView)view.findViewById(R.id.number);
            commandTitle = (TextView)view.findViewById(R.id.command_title);
            command = (TextView)view.findViewById(R.id.command);
            startTime = (TextView)view.findViewById(R.id.start_time);
            endTime = (TextView)view.findViewById(R.id.end_time);
            edit = (Button)view.findViewById(R.id.edit);
            delete = (Button)view.findViewById(R.id.delete);
        }
    }

    public ControlPersonAdapter(List<ControlPerson> controlPerson){
        this.controlPerson = controlPerson;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_control,parent,false);
        context = parent.getContext();
        ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return  holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.name.setText(controlPerson.get(position).name);
        holder.number.setText(controlPerson.get(position).number);
        holder.command.setText(controlPerson.get(position).command);
        holder.startTime.setText(controlPerson.get(position).startTime);
        holder.endTime.setText(controlPerson.get(position).endTime);
        if(controlPerson.get(position).isInControl){
            holder.command.setTextColor(ContextCompat.getColor(context,R.color.car_normal));
            holder.commandTitle.setTextColor(ContextCompat.getColor(context,R.color.car_normal));
        }
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo getong 26367 这里要打开新的界面编辑
                Toast.makeText(context,"编辑",Toast.LENGTH_SHORT).show();
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo getong 26367 做删除
                Toast.makeText(context,"删除",Toast.LENGTH_SHORT).show();
            }
        });
    }


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


}

 第一次用时候有个坑,自动implements的getItemCount返回值默认是0,导致代码不会走onBindViewHolder(),就一条item都不会显示。

 

代码效果:

RecyclerView是一个高度可定制的列表视图组件,它在Android开发中常用于展示大量数据。以下是使用RecyclerView的基本步骤: 1. **添加依赖**: - 在Gradle构建脚本中添加RecyclerView的依赖,如果是使用AndroidX,则在app模块的build.gradle文件中加入: ```gradle implementation 'com.google.android.material:material:1.4.0' ``` - 或者对于老版的support库,添加: ```gradle implementation 'com.android.support:recyclerview-v7:28.0.0' ``` 2. **添加布局文件**: 创建一个XML布局文件,例如`item_recycler_view.xml`,定义每个列表项的外观。 3. **在Activity/Fragment中声明 RecyclerView**: 在对应的布局文件中添加RecyclerView,指定其ID和一些基本属性,如宽度和高度。 ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 4. **创建Adapter**: 创建一个继承自`RecyclerView.Adapter`的自定义适配器,负责管理数据源和视图的绑定。你需要覆盖` onCreateViewHolder()`, ` onBindViewHolder()` 和 ` getItemCount()` 方法。 5. **设置Adapter**: 在Activity/Fragment中,找到RecyclerView并设置Adapter。 ```java RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setAdapter(new MyAdapter()); ``` 6. **设置LayoutManager**: 根据需求选择合适的LayoutManager,如 LinearLayoutManager, GridLayoutManager 等。设置在Adapter的构造函数中或者在Activity/Fragment中设置。 ```java recyclerView.setLayoutManager(new LinearLayoutManager(this)); ``` 7. **加载数据**: 将数据填充到适配器中,可以在Adapter的构造函数中完成,也可以在onStart或onCreateView方法里动态加载。 8. **设置ItemAnimator** (可选): 可以增强动画效果,如: ```java recyclerView.setItemAnimator(new DefaultItemAnimator()); ``` 9. **滚动监听事件** (可选): 添加OnScrollListener来响应滚动事件,例如分页加载更多。 至此,就完成了基础的RecyclerView使用。你可以进一步个性化设置,如监听点击事件、加载更多等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值