RecyclerView使用_recyclerview是什么

RecyclerView使用_recyclerview是什么本文目录RecyclerView概述RecyclerView使用-基础篇第一步:添加RecyclerView第二步:添加布局文件第三步:添加逻辑代码运行效果RecyclerView使用-进阶篇布局管理器线性布局管理器网格布局管理器ItemDecorationItem动画Item点击RecyclerView使用-高级篇(上拉刷新下拉加载更多)第一步:添加依赖第二步:布局文件第三步:逻辑代码完整代…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

RecyclerView概述

RecyclerView是官方在5.0之后新添加的控件,推出用来替代传统的ListViewGridView列表控件。

RecyclerView使用-基础篇

第一步:添加RecyclerView

**方法1:**点开任意一个布局文件,找到左边的RecyclerView控件,点击旁边的按钮即可添加RecyclerView
在这里插入图片描述
**方法2:**在build.gradle文件中添加依赖

    implementation 'androidx.recyclerview:recyclerview:1.1.0'

Jetbrains全家桶1年46,售后保障稳定

在这里插入图片描述

第二步:添加布局文件

在布局文件activity_main.xml中先添加RecyclerView控件

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

再添加一个item_list.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="29dp"
        android:layout_marginTop="33dp"
        android:text="标题"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="18dp"
        android:text="内容"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

第三步:添加逻辑代码

首先定一个实体类如下:

public class News { 
   
    public String title; // 标题
    public String content; //内容
}

然后在MainActivity中定义内部类 ViewHolder类、 MyAdapter类以及设置RecyclerView相关逻辑

package com.lucashu.recyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity { 
   
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<News> mNewsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = findViewById(R.id.recyclerview);
        // 构造一些数据
        for (int i = 0; i < 50; i++) { 
   
            News news = new News();
            news.title = "标题" + i;
            news.content = "内容" + i;
            mNewsList.add(news);
        }
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> { 
   

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 
   
            View view = View.inflate(MainActivity.this, R.layout.item_list, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) { 
   
            News news = mNewsList.get(position);
            holder.mTitleTv.setText(news.title);
            holder.mTitleContent.setText(news.content);
        }

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

    class MyViewHoder extends RecyclerView.ViewHolder { 
   
        TextView mTitleTv;
        TextView mTitleContent;

        public MyViewHoder(@NonNull View itemView) { 
   
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.textView);
            mTitleContent = itemView.findViewById(R.id.textView2);
        }
    }
}

运行效果

在这里插入图片描述

RecyclerView使用-进阶篇

布局管理器

RecyclerView提供了三种布局管理器即:

  • LinearLayoutManager 线性布局管理器
  • StaggeredGridLayoutManager 瀑布流布局管理器
  • GridLayoutManager 网格布局管理器

线性布局管理器

这三种布局管理器都是通过setLayoutManager方法来设置
LinearLayoutManager 还可以设置横向滚动,只需将前面MainActivity中的layoutManager加一句代码即可:

 LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
 layoutManager.setOrientation(RecyclerView.HORIZONTAL);
 mRecyclerView.setLayoutManager(layoutManager);

在这里插入图片描述

网格布局管理器

如果让一行显示多个,可以设置 GridLayoutManager网格布局管理器来实现

  GridLayoutManager layoutManager = new GridLayoutManager(MainActivity.this,3);
// layoutManager.setOrientation(RecyclerView.HORIZONTAL); 也能设置横向滚动
  mRecyclerView.setLayoutManager(layoutManager);

在这里插入图片描述

ItemDecoration

通过给 设置ItemDecoration 来装饰Item的效果,比如我们要设置间隔线

DividerItemDecoration mDivider = new    
                        DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
mRecyclerView.addItemDecoration(mDivider);

在这里插入图片描述

Item 动画

RecyclerView提供了默认的ItemAnimator实现类:DefaultItemAnimator。该类可以帮我们实现一些炫酷的动画效果

DefaultItemAnimator itemAnimator = new DefaultItemAnimator();
defaultItemAnimator.setAddDuration(1000);
defaultItemAnimator.setRemoveDuration(1000);
 mRecyclerView.setItemAnimator(itemAnimator);

MainActivity中添加了两个按钮, 一个添加item,一个删除item

Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() { 
   
            @Override
            public void onClick(View v) { 
   
                News news = new News();
                news.title = "标题 新内容" ;
                news.content = "内容 新内容" ;
                mNewsList.add(1,news);
                mMyAdapter.notifyItemInserted(1);
            }
        });
        Button button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() { 
   
            @Override
            public void onClick(View v) { 
   
                mNewsList.remove(0);
                mMyAdapter.notifyItemMoved(0,1);
            }
        });

效果如下:
在这里插入图片描述

Item 点击

可以通过对整个Item文件的根布局添加一个点击事件来实现Item的点击

在这里插入图片描述

在这里插入图片描述

效果如下:
在这里插入图片描述

RecyclerView使用-高级篇(上拉刷新下拉加载更多)

通常RecyclerView使用的时候需要配合上拉刷新下拉加载更多的功能
我们通过开源控件SmartRefreshLayout来实现
SmartRefreshLayout官网:https://github.com/scwang90/SmartRefreshLayout
使用方法如下:

第一步:添加依赖

在gradle文件中添加

implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2'   
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.2'  

第二步:布局文件

activity_main.xmlSmartRefreshLayout控件将RecyclerView包裹起来

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

第三步:逻辑代码

MainActivityonCreate方法中添加下拉刷新和上拉加载更多的功能

下拉刷新

RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() { 
   
            @Override
            public void onRefresh(RefreshLayout refreshlayout) { 
   
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                mNewsList.clear();
                for (int i = 0; i < 10; i++) { 
   
                    News news = new News();
                    news.title = "标题 新内容" + i;
                    news.content = "内容" + i;
                    mNewsList.add(news);
                }
                mMyAdapter.notifyDataSetChanged();
            }
        });

上拉加载更多

 refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { 
   
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) { 
   
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                for (int i = 0; i < 10; i++) { 
   
                    News news = new News();
                    news.title = "标题 新内容" + i;
                    news.content = "内容" + i;
                    mNewsList.add(news);
                }
                mMyAdapter.notifyDataSetChanged();
            }
        });

效果如下:
在这里插入图片描述

完整代码

MainActivity.java文件

package com.lucashu.recyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity { 
   
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<News> mNewsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = findViewById(R.id.recyclerview);
        // 构造一些数据
        for (int i = 0; i < 10; i++) { 
   
            News news = new News();
            news.title = "标题" + i;
            news.content = "内容" + i;
            mNewsList.add(news);
        }

        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(layoutManager);


        RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() { 
   
            @Override
            public void onRefresh(RefreshLayout refreshlayout) { 
   
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                mNewsList.clear();
                for (int i = 0; i < 10; i++) { 
   
                    News news = new News();
                    news.title = "标题 新内容" + i;
                    news.content = "内容" + i;
                    mNewsList.add(news);
                }
                mMyAdapter.notifyDataSetChanged();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { 
   
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) { 
   
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                for (int i = 0; i < 10; i++) { 
   
                    News news = new News();
                    news.title = "标题 新内容" + i;
                    news.content = "内容" + i;
                    mNewsList.add(news);
                }
                mMyAdapter.notifyDataSetChanged();
            }
        });

    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> { 
   

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 
   
            View view = View.inflate(MainActivity.this, R.layout.item_list, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, final int position) { 
   
            News news = mNewsList.get(position);
            holder.mTitleTv.setText(news.title);
            holder.mTitleContent.setText(news.content);
            holder.mRootView.setOnClickListener(new View.OnClickListener() { 
   
                @Override
                public void onClick(View v) { 
   
                    Toast.makeText(MainActivity.this,"点击了:"+ position,Toast.LENGTH_LONG).show();
                }
            });
        }

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

    class MyViewHoder extends RecyclerView.ViewHolder { 
   
        TextView mTitleTv;
        TextView mTitleContent;
        ConstraintLayout mRootView;

        public MyViewHoder(@NonNull View itemView) { 
   
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.textView);
            mTitleContent = itemView.findViewById(R.id.textView2);
            mRootView = itemView.findViewById(R.id.rootview);
        }
    }
}

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203976.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android Intent.FLAG_ACTIVITY_NEW_TASK的个人理解「建议收藏」

    Android Intent.FLAG_ACTIVITY_NEW_TASK的个人理解「建议收藏」首先分四部曲简单做一下说明1.What(是什么):Intent类中的一个静态标志属性publicstaticfinalintFLAG_ACTIVITY_NEW_TASK=0x10000000;2.Why(为什么要使用):在特殊情况下,如果不加这个标志,会报错(下文详细说明)3.When(什么时候使用):当调用startActivity启动一个Activity时4.How(如何

    2022年10月6日
    2
  • 简述ip地址的abc类如何划分_IP 地址分类及 ABCDE 五类是如何划分的

    简述ip地址的abc类如何划分_IP 地址分类及 ABCDE 五类是如何划分的我们先了解下什么是IP地址,英文全称为:InternetProtocolAddress,缩写为:IPAdress,这是在网络上给主机统一编址的一种地址格式,也称为网络协议(IP协议)地址。它为互联网上的每一个网络和主机分配一个逻辑地址,而IP地址分为IPv4与IPv6两个类别,目前应用较多的是IPv4,当资源耗尽时自然会升级到IPv6。什么是ip地址IP地址由四个段组成,每个字段是一个字节,…

    2022年5月15日
    32
  • 2021MySql-8.0.26安装详细教程(保姆级)

    2021MySql-8.0.26安装详细教程(保姆级)MySql-8.0.26安装详细教程保姆级下载安装包安装配置配置环境变量下载安装包下载安装包:下载网址:https://dev.mysql.com/downloads/选择这个进入后选择直接下载第一个点击这里,开始下载安装配置解压安装包我这里解压到d盘打开编写MySQL配置文件在解压目录下新建my.ini文件将下面文本拷贝进my,ini文件中[mysqld]#设置3306端口port=3306#设置mysql的安装目录———-是你的文件路径-

    2022年4月28日
    47
  • Java基础知识点笔记(一):java中的取整与四舍五入

    Java基础知识点笔记(一):java中的取整与四舍五入一.java种取整数的方式1.直接使用强制转换publicstaticvoidroundOne(){System.out.println(“正数:(int)10.12=”+(int)10.12);System.out.println(“负数:(int)-10.12=”+(int)-10.12);S

    2022年7月8日
    25
  • 美国城市地名简称_外国地名英文

    美国城市地名简称_外国地名英文近期在做某个项目要用到美国的地名,上网查了一圈都没有比較具体的、专业的,仅仅好自己复制了一个大概有500多个城市、城镇的英文,用谷歌翻译一下,结果例如以下:谷歌翻译结果,非常多是错误的,边用边改美国地

    2022年8月2日
    2
  • Python 十进制转换为二进制 高位补零

    Python 十进制转换为二进制 高位补零用到的内置函数为 format 具体方法如下 gt gt gt a 2 gt gt gt b 08b format a 输出结果为八位二进制 且高位补零 如果是 gt gt gt b 8b format a 输出结果为八位二进制 但是高位不补零 需要注意的是 输出的结果类型都为 str 类型

    2025年7月31日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号