Android之RecyclerView仿ViewPage滑动

文章目录

  • 前言
  • 一、效果图
  • 二、实现步骤
    • 1.xml主布局
    • 2.所有用到的drawable资源文件
    • 3.xml item布局
    • 4.adapter适配器
    • 5.javabean实体类
    • 6.activity使用
  • 总结


前言

我们都知道ViewPage+Fragment滑动,但是的需求里面已经有了这玩意,但是在Fragment中还要有类似功能,这时我相信很多人就苦恼了,没事,这张来解决,用RecyclerView去实现即可,而且还带指示器。


一、效果图

这里我没有弄GIF,反正效果和ViewPage+Fragment是一样的。
在这里插入图片描述

二、实现步骤

1.xml主布局

代码如下(示例):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#f8f8f8"android:orientation="vertical"><RelativeLayoutandroid:id="@+id/relat_title"android:layout_width="match_parent"android:layout_height="80dp"android:background="#ffffff"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="15dp"android:text="Machine"android:textColor="#232323"android:textSize="20dp"android:textStyle="bold" /></RelativeLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/list_view"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/rel_bt"android:layout_below="@+id/relat_title"android:layout_marginLeft="16dp"android:layout_marginTop="15dp"android:layout_marginRight="16dp"android:layout_marginBottom="15dp" /><RelativeLayoutandroid:id="@+id/rel_bt"android:layout_width="match_parent"android:layout_height="140dp"android:layout_alignParentBottom="true"><LinearLayoutandroid:id="@+id/linear_list"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:gravity="center"android:orientation="horizontal" /><TextViewandroid:id="@+id/text_gm"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginRight="32dp"android:background="@drawable/back_hs_qy"android:gravity="center"android:text="购买\n咨询"android:textColor="#ffffff"android:layout_marginTop="20dp"android:textSize="12dp"android:textStyle="bold" /></RelativeLayout></RelativeLayout>

2.所有用到的drawable资源文件

一个是主布局购买按钮,一个是item布局的背景。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 背景颜色 --><solid android:color="@color/bjs" /><sizeandroid:width="60dp"android:height="60dp" /><!-- 控制圆角大小 --><corners android:radius="50dp" /></shape><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 背景颜色 --><solid android:color="#ffffff" /><!-- 控制边界线颜色和大小 --><strokeandroid:width="1dp"android:color="#ffffff" /><!-- 控制圆角大小 --><corners android:radius="16dp" /></shape>

该处使用的url网络请求的数据。

3.xml 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="match_parent"android:background="#f8f8f8"android:gravity="center"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="16dp"android:background="@drawable/bzhs_ff_16"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background="@color/bjs"android:paddingLeft="15dp"android:paddingTop="3dp"android:paddingRight="15dp"android:paddingBottom="3dp"android:text="在售主力机型"android:textColor="#ffffff"android:textSize="14dp" /><TextViewandroid:id="@+id/textname"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="30dp"android:text="神马M50S"android:textColor="#232323"android:textSize="20dp"android:textStyle="bold" /><ImageViewandroid:layout_width="match_parent"android:layout_height="180dp"android:layout_marginTop="20dp"android:background="#262626" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="30dp"android:text="128T | 3328W | 26J/T"android:textColor="#232323"android:textSize="16dp" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="20dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="价格  "android:textColor="#232323"android:textSize="16dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="$1234433"android:textColor="#E88009"android:textSize="16dp" /></LinearLayout></LinearLayout></LinearLayout>

4.adapter适配器

/*** 作者:CaoLiulang* ❤* Date:2023/6/12* ❤* 模块 账单列表adapter*/
public class KuangAdapter extends RecyclerView.Adapter<KuangAdapter.ViewHolder> {private List<KuangBean> list;private Context context;public KuangAdapter(List<KuangBean> list, Context context) {this.list = list;this.context = context;}/*** 加载更多** @param mPageList*/public void setData(List<KuangBean> mPageList) {try {if (mPageList != null) {int previousSize = 0;try {previousSize = list.size();} catch (Exception e) {previousSize = 0;}int sizez = previousSize + 2;list.addAll(mPageList);notifyItemRangeInserted(sizez, mPageList.size());}} catch (Exception e) {e.printStackTrace();}}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_item, parent, false);ViewHolder viewHolder = new ViewHolder(view);return viewHolder;}/*** 类似GetView** @param holder* @param position*/@Overridepublic void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {holder.textname.setText(list.get(position).name);}//添加元素,需要告诉UI线程布局的变动public void update() {notifyDataSetChanged();}/*** 长度** @return*/@Overridepublic int getItemCount() {return list.size();}/*** 初始化组件*/class ViewHolder extends RecyclerView.ViewHolder {TextView textname;public ViewHolder(final View itemView) {super(itemView);textname = itemView.findViewById(R.id.textname);}}
}

5.javabean实体类

public class KuangBean {public String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "KuangBean{" +"name='" + name + '\'' +'}';}
}

6.activity使用

class KuangFragment : BaseFragment(), View.OnClickListener {private lateinit var list_view: RecyclerViewprivate lateinit var adapter: KuangAdapterprivate lateinit var list: MutableList<KuangBean>private lateinit var linear_list: LinearLayoutprivate lateinit var text_gm: TextViewprivate var xb = 0fun newInstance(bundle: Bundle?): KuangFragment? {val fragment =KuangFragment()if (bundle != null) {fragment.arguments = bundle}return fragment}override fun getContentViewId(): Int {return R.layout.fragment_kuang}override fun initView(savedInstanceState: Bundle?) {list = mutableListOf()for (i in 1..4) {var bean = KuangBean()bean.setName("神马M50S$i")list.add(bean)}list_view = rootView!!.findViewById(R.id.list_view)linear_list = rootView!!.findViewById(R.id.linear_list)text_gm = rootView!!.findViewById(R.id.text_gm)list_view.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false) //竖向显示adapter = KuangAdapter(list, activity)list_view.adapter = adapter// 设置加载更多监听list_view.addOnScrollListener(object : EndlessRecyclerOnScrollListener() {override fun onLoadMore(newState: Int) {if (newState >= 0) {setDate(newState)}}})val snapHelper = LinearSnapHelper()snapHelper.attachToRecyclerView(list_view)text_gm.setOnClickListener(this)setDate(0)}private fun setDate(po: Int) {xb = po//清空Viewlinear_list.removeAllViews()for (i in 0 until list.size) {val flview =LayoutInflater.from(activity).inflate(R.layout.guild_itme, null) as LinearLayoutval text_id: TextView = flview.findViewById(R.id.text_id)if (po == i) {text_id.setBackgroundResource(R.drawable.back_hs_12)} else {text_id.setBackgroundResource(R.drawable.back_hs1_12)}linear_list.addView(flview)}}override fun onClick(v: View?) {when (v?.id) {R.id.text_gm -> {}}}

总结

感觉就像买东西一样,物美价廉,值得推荐使用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/76018.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于3D扫描和3D打印的产品逆向工程实战【数字仪表】

逆向工程是一种从物理零件创建数字设计的强大方法&#xff0c;并且可以与 3D 扫描和 3D 打印等技术一起成为原型设计工具包中的宝贵工具。 推荐&#xff1a;用 NSDT编辑器 快速搭建可编程3D场景 3D 扫描仪可以非常快速地测量复杂的物体&#xff0c;并且在涉及现实生活参考时可以…

花生壳内网穿透+Windows系统,如何搭建网站?

1. 准备工作 在百度搜索“Win7下安装ApachePHPMySQL”&#xff0c;根据搜到的教程自行安装WAMP环境。 如果在网页上键入http://127.0.0.1/ 出现以下页面表示您的服务器已经建好&#xff0c;下一步就是关键&#xff0c;如何通过花生壳内网穿透&#xff0c;让外网的用户访问到您…

ElementUI浅尝辄止38:Upload 上传

通过点击或者拖拽上传文件实现上传功能&#xff0c;常见于文件、文件夹或图片上传&#xff0c;使用挺频繁的。需要熟练掌握 1.如何使用&#xff1f;点击上传 通过 slot 你可以传入自定义的上传按钮类型和文字提示。可通过设置limit和on-exceed来限制上传文件的个数和定义超出限…

设计模式 - 责任链

一、前言 ​ 相信大家平时或多或少都间接接触过责任链设计模式&#xff0c;只是可能有些同学自己不知道此处用的是该设计模式&#xff0c;比如说 Java Web 中的 Filter 过滤器&#xff0c;就是非常经典的责任链设计模式的例子。 那么什么是责任链设计模式呢&#xff1f; ​ …

SAFe大规模敏捷框架,敏捷认证培训体系(全)

1. Leading SAFe 课程受众&#xff1a;课程面向决策层、领导者和经理。课程目标&#xff1a;成为一名具备精益敏捷思维的领导者&#xff0c;通过系统化地学习 SAFe&#xff0c;能够领导企业级业务敏捷转型&#xff0c;通过设计思维理解客户需求&#xff0c;实施敏捷产品交付、…

大数据课程L6——网站流量项目的SparkStreaming

文章作者邮箱:yugongshiye@sina.cn 地址:广东惠州 ▲ 本章节目的 ⚪ 了解网站流量项目的SparkStreaming概述; ⚪ 掌握网站流量项目的SparkStreaming实现 Wordcount 底层流程; ⚪ 掌握网站流量项目的SparkStreaming实现历史批次的累积处理; ⚪ 掌握网站流…

快速学会git版本管理——上传gitee仓库

首先在gitee右上角有一个新建仓库 创建之后打开自己想要上传的文件 右键打开 Git Bash Here 接下来会弹出git的窗口 首先先初始化仓库 用git命令 git init 然后用git add . 上传所有文件上传到暂存区(上一篇文章说过add是单个文件&#xff0c;add . 是所有文件) 没有显示错误 …

算法训练营第四十四天(9.6)| 动态规划Part17

目录 Leecode 647.回文子串 Leecode 516.最长回文子序列 Leecode 647.回文子串 题目地址&#xff1a;力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 题目类型&#xff1a;回文 class Solution { public:int countSubstrings(string s) {int n s.si…

OpenCV_CUDA_VS编译安装

一、OpenCV 我这里是下载的OpenCV4.5.4&#xff0c;但是不知道到在vs里面build时一直报错&#xff0c;后面换了4.7.0的版本测试&#xff0c;安装成功。 Release OpenCV 4.5.4 opencv/opencv GitHub 这个里面有官方预编译好的OpenCV库&#xff0c;可以直接食用。 扩展包&am…

Python数据类型的相互转换

简单数据类型之间的转换 1.字符串如果是数字的&#xff0c;转换为int类型 a "10" a int(a) print(a) 2.数字类型转换成bool类型 a 10 a bool(a) print(a) 只有0才是false&#xff0c;其他值是True 复杂数据类型之间的转换 list&#xff1a;列表 tuple&…

SQL4 查询结果限制返回行数

描述 题目&#xff1a;现在运营只需要查看前2个用户明细设备ID数据&#xff0c;请你从用户信息表 user_profile 中取出相应结果。 示例&#xff1a; iddevice_idgenderageuniversityprovince12138male21北京大学Beijing23214male复旦大学Shanghai36543female20北京大学Beijin…

Golang 中的静态类型和动态类型

定义说明 静态类型&#xff08;static type&#xff09;&#xff1a;在编码时就能确定的类型&#xff0c;通过变量定义可以确定的类型&#xff1b;动态类型&#xff08;concrete type&#xff09;&#xff1a;在运行时才能确定具体的数据类型&#xff1b; 动态静态类型如何理…

设计模式(1) - UML类图

1、前言 从这一节开始&#xff0c;我们将一起学习设计模式。我们的学习目标是什么呢&#xff1f; 了解常用设计模式以及它们的使用场景&#xff1b;分析实际工程中设计模式的使用&#xff0c;揣摩实际意图&#xff0c;了解作者设计思路&#xff1b;尝试运用设计模式迭代、重构…

css transition 指南

css transition 指南 在本文中&#xff0c;我们将深入了解 CSS transition&#xff0c;以及如何使用它们来创建丰富、精美的动画。 基本原理 我们创建动画时通常需要一些动画相关的 CSS。 下面是一个按钮在悬停时移动但没有动画的示例&#xff1a; <button class"…

qt之movetothread理解

基础概念 qt的下线程qthread&#xff0c;每个线程都有自己的事件循环exec。对象的线程上下文&#xff0c;每个对象都有自己的线程上下文&#xff0c;怎么理解呢&#xff0c;就是该对象在哪个线程创建&#xff0c;其线程上下文就是谁。每个qobject对象在创建时都有包含线程成员…

MySQL下载安装环境变量配置,常用命令

一、下载安装 mysql官网 下载连接 这个是下载图形安装 https://dev.mysql.com/downloads/installer/ 这个是下载免图形安装 https://dev.mysql.com/downloads/mysql/ 担心个别宝宝没有账号&#xff0c;这边也提供一下&#xff0c;方便下载&#xff1a; 账户&#xff1a;1602404…

Spring Cloud 面试题总结

Spring Cloud和各子项目版本对应关系 Spring Cloud 是一个用于构建分布式系统的开发工具包&#xff0c;它基于Spring Boot提供了一组模块和功能&#xff0c;用于构建微服务架构中的分布式应用程序。Spring Cloud的不同子项目有各自的版本&#xff0c;下面是一些常见的Spring C…

使用 Pandera 的 PySpark 应用程序的数据验证

推荐&#xff1a;使用 NSDT场景编辑器 快速搭建3D应用场景 本文简要介绍了 Pandera 的主要功能&#xff0c;然后继续解释 Pandera 数据验证如何与自最新版本 &#xff08;Pandera 0.16.0&#xff09; 以来使用本机 PySpark SQL 的数据处理工作流集成。 Pandera 旨在与其他流行…

linux 压缩webfile文件夹 webfile.tar.gz和webfile.tar的区别

linux 压缩webfile文件夹 在Linux中&#xff0c;你可以使用tar命令来压缩文件夹。以下是将文件夹压缩为名为"webfile.tar"的示例命令&#xff1a; cd到webfile所在的文件夹&#xff0c;然后执行 tar -cvf webfile.tar webfile/上述命令中&#xff0c;-c选项表示创建…

DNS(域名解析系统)

含义 当我们在上网要访问莫个服务器的时候&#xff0c;就需要知道服务器的IP地址&#xff0c;但IP地址是一串数字&#xff0c;虽然这串数字用点分十进制已经清晰不少了&#xff0c;但还是不利于人们记忆和传播&#xff0c;于是人们使用单词来代替IP地址&#xff08;例如baidu&a…