RecyclerView实现横向滚动效果
发布时间:2023-11-24 02:26:30
标签:RecyclerView,横向滚动
本文实例为大家分享了RecyclerView实现横向滚动效果的具体代码,供大家参考,具体内容如下
布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"/>
</LinearLayout>
Item
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<ImageView
android:id="@+id/iv_recyclerview_imag"
android:layout_width="wrap_content"
android:layout_height="100dp" />
<TextView
android:id="@+id/tv_recyclerview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="老虎"
android:textSize="17sp"
android:layout_gravity="center"
android:textStyle="bold"
android:padding="3dp"/>
</LinearLayout>
适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<Animal> animalList;
private int resource;
public RecyclerViewAdapter(List<Animal> animalList, int resource) {
this.animalList = animalList;
this.resource = resource;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(resource,parent,
false);
ViewHolder holder = new ViewHolder(itemView);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Animal animal = animalList.get(position);
holder.animalImag.setImageResource(animal.getImageId());
holder.animalName.setText(animal.getName());
}
@Override
public int getItemCount() {
return animalList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView animalImag;
TextView animalName;
public ViewHolder(View itemView){
super(itemView);
animalImag = itemView.findViewById(R.id.iv_recyclerview_imag);
animalName = itemView.findViewById(R.id.tv_recyclerview_name);
}
}
}
核心代码
public class RecyclerViewActivity extends AppCompatActivity {
private List<Animal> animalList = new ArrayList<>();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
recyclerView = findViewById(R.id.recyclerView_view);
initAnimals();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(animalList,R.layout.recyclerview_item);
recyclerView.setAdapter(adapter);
}
//初始化动物数据
private void initAnimals() {
Animal daxaing = new Animal("大象", R.drawable.animal_one);
animalList.add(daxaing);
Animal shizi = new Animal( "袋鼠", R.drawable.animal_two);
animalList.add(shizi);
Animal daishu = new Animal("二哈", R.drawable.animal_three);
animalList.add(daishu);
Animal laohu = new Animal("狮子", R.drawable.animal_four);
animalList.add(laohu);
Animal zhu = new Animal("猪", R.drawable.animal_five);
animalList.add(zhu);
Animal songshu = new Animal("猴子", R.drawable.animal_six);
animalList.add(songshu);
Animal baozi = new Animal("豹子", R.drawable.animal_seven);
animalList.add(baozi);
Animal shayu = new Animal("鲨鱼", R.drawable.animal_eight);
animalList.add(shayu);
}
}
来源:https://blog.csdn.net/weixin_50506453/article/details/111941344


猜你喜欢
- public final class Integer extends Number implements Comparable<Int
- 前言有时候我们在项目中,会用到一些本地 jar 包文件,比如隔壁公司自己打包的;此时无法从maven远程仓库拉取;那么我们可以考虑把 jar
- 混淆studio 使用Proguard进行混淆,其是一个压缩、优化和混淆java字节码文件的一个工具。功能:Shrinking(压缩)、Op
- 1.首先看下我的项目结构我们逐个讲解/** * 用户登录配置类 * @author Administrator * */public cla
- 本文实例讲述了Java使用反射调用方法。分享给大家供大家参考,具体如下:一 代码import java.util.*;import java
- 一、什么是 RestTemplate?RestTemplate是执行HTTP请求的同步阻塞式的客户端,它在HTTP客户端库(例如JDK Ht
- 前言最近做项目框架,需要在框架结束的时候,关闭服务器连接,清除部分框架运行lock文件,这里就想到了shutdownhook,顺便学了学Ru
- 本文讨论了Spring Data JDBC如何实现DDD中聚合根存储的设计思路,其中主要讨论了是不是每个实体都需要一个对应数据表,这种问题需
- 今天因为发布swagger-spring-boot-starter做一个问题的修复,然后碰到了下面这个问题,记录一下解决过程,帮助后续碰到类
- 本文实例为大家分享了Java手写线程池的实现代码,供大家参考,具体内容如下1.线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在
- 1.分支结构的概念当需要进行条件判断并做出选择时,使用分支结构2.if分支结构格式:if(条件表达式){语句块;}package com.l
- 一般有点开发经验的朋友都能实现这样的功能,只不过是效率上的问题。我们一般在面对这样的问题时,总会平铺直序的联想到,先生成一个数组,然后在一个
- 1、使用排序2、原理事实上Collections.sort方法底层就是调用的array.sort方法,而且不论是Collections.so
- 一、前言点关注不迷路,持续输出Unity干货文章。嗨,大家好,我是新发。之前我写了一篇Unity流体模拟的文章:《Unity流体模拟,支持粒
- spring security用了也有一段时间了,弄过异步和多数据源登录,也看过一点源码,最近弄rest,然后顺便搭oauth2,前端用js
- 1.特殊数组的特征值题目描述思路详解看到本题,首先思考需要排序,然后查找,这里为了效率采用二分查找。假设定义x=(left+riht)/ 2
- 1 常量定义在程序中存在大量的数据来代表程序的状态,其中有些数据在程序运行过程中值不能发生改变,这些数据在程序中被叫做常量。2 常量语法命名
- 问:怎样才能将XML文件导入SQL Server 2000? 答:将XML文件导入SQL Server有若干种方法,这里提供其中的3种: 大
- 详解Kotlin的空指针处理Kotlin的空指针处理相比于java有着极大的提高,可以说是不用担心出现NullPointerExceptio
- 单个和批量定义别名typeAliases使用Mybatis的别名typeAliases可以在xml文件里非常方便的使用类,而不需要写出这个类