Android控件RecyclerView的基本用法

RecyclerView

RecycleView是Android5.0后谷歌推出的一个用于在有限的窗口中展示大量数据集的控件。它可以实现与ListView和GridView一样的效果,提供了一种插拔式的体验,高度的解耦,异常的灵活,只需设置其提供的不同的LayoutManager,ItemAnimator和ItemDecoration,就能实现不同的效果。

RecyclerView的优点

  1. 支持局部刷新
  2. 可以自定义item增删时的动画
  3. 能够实现item拖拽和侧滑删除等功能
  4. 默认已实现View的复用,而且回收机制更加完善

使用方法

布局<主界面Activity>

    <LinearLayout
        android:layout_width="336dp"
        android:layout_height="366dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="14dp"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button04">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list001"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="48dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp" />
    </LinearLayout>

创建一个xml,做嵌入使用

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/newsPic"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:adjustViewBounds="false" />

        <TextView
            android:id="@+id/newsTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:text="TextView" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

创建一个adapter类(主要的代码)

/*继承RecyclerView.Adapter
* NewsAdapter.ViewHolder 是一个内部static类,继承了
* RecyclerView.ViewHolder
*/
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private List<News> newsList;
    //定义一个OnItemClickListener接口,这个接口是自定义的
    private OnItemClickListener onItemClickListener;

//定义OnItemClickListener的操作方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

	//构造函数,将需要显示的数组放进去。数据一般存储对象,对象里面定义需要显示的元素。在onBindViewHolder取出来使用
    public NewsAdapter(List<News> newsList) {
        this.newsList = newsList;
    }
//创建ViewHolder,后续item布局里控件都是从ViewHolder中取出
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item,viewGroup,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

//绑定操作
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int i) {
        News news = newsList.get(i);
        viewHolder.newsImage.setImageResource(news.getPic());
        viewHolder.newsTitle.setText(news.getTitle());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(v,viewHolder.getAdapterPosition()+1);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }
/**
* 自定义的ViewHolder
*/
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView newsImage;
        TextView newsTitle;
        public ViewHolder(View itemView) {
            super(itemView);
            newsImage = itemView.findViewById(R.id.newsPic);
            newsTitle = itemView.findViewById(R.id.newsTitle);
        }
    }
}

主程序:

//自定义的一个class,比较随意
List<News> newList = new ArrayList();
newList.add(new News("新闻1", R.drawable.pic001));
newList.add(new News("新闻2", R.drawable.pic001));
newList.add(new News("新闻3", R.drawable.pic001));
newList.add(new News("新闻4", R.drawable.pic001));
newList.add(new News("新闻5", R.drawable.pic001));

//创建一个NewsAdapter,将List放进去
NewsAdapter newsAdapter = new NewsAdapter(newList);
//实现OnClick操作
newsAdapter.setOnItemClickListener(new NewsAdapter.OnItemClickListener(){
            public void onItemClick(View view, int position){
                Toast.makeText(getApplicationContext(),"第" + position + "条数据", Toast.LENGTH_SHORT).show();
            }
        });
//获取控件
recView = findViewById(R.id.list001);
//创建一个LinearLayoutManager
LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
//设置控件布局
recView.setLayoutManager(layoutmanager);
//将Adapter绑到控件上
recView.setAdapter(newsAdapter);
//设置水平方向的隔离线
recView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

添加和删除条目
在Adapter里添加code

    /* 添加数据*/
    public void addItem() {
        newsList.add(new News("新的", R.drawable.pic001));
        notifyItemInserted(0);
    }

    /* 移除数据*/
    public void removeItem(int position) {
        newsList.remove(position);
        notifyItemRemoved(position);
    }

在主程序开启动态功能

recView.setItemAnimator(new DefaultItemAnimator());

自己画两个Button,执行addItem和removeItem即可!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值