RecyclerView实现横向滚动效果

时间:2023-11-24 02:26:30 

本文实例为大家分享了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

标签:RecyclerView,横向滚动
0
投稿

猜你喜欢

  • Log4j.properties配置及其使用

    2023-05-14 21:06:18
  • java对double数组排序示例分享

    2022-07-13 03:22:14
  • JAVA常用API总结与说明

    2023-05-19 08:17:17
  • Jackson中json格式的字符串与对象的互相转换方式

    2022-01-29 03:31:07
  • 聊聊如何打印GC日志排查的问题

    2023-01-22 22:10:56
  • Spring存储与读取Bean对象方法

    2021-11-12 03:35:27
  • C#中的Linq To XML讲解

    2021-06-29 18:39:43
  • 详解Spring Cloud中Hystrix的请求合并

    2022-07-06 14:53:06
  • Java并发线程池实例分析讲解

    2022-08-05 20:25:40
  • java仿QQ微信聊天室功能的实现

    2022-05-14 17:44:28
  • 详解Android中Activity运行时屏幕方向与显示方式

    2022-02-12 03:51:06
  • Android 实现悬浮窗功能

    2021-09-17 20:02:07
  • Java转JSON串的几种方式

    2023-08-24 07:38:47
  • PageHelper在springboot+mybatis框架中的使用步骤及原理解析

    2023-07-28 21:40:09
  • mac系统下载、安装、使用Java8教程

    2023-07-06 04:54:38
  • android打开本地图像的方法

    2022-10-26 08:01:42
  • SpringBoot使用jsr303校验的实现

    2022-04-21 04:02:39
  • Flutter刷新组件RefreshIndicator自定义样式demo

    2023-07-06 15:56:45
  • C#使用HttpPost请求调用WebService的方法

    2022-02-24 07:22:16
  • SpringBoot深入浅出分析初始化器

    2022-07-06 09:05:59
  • asp之家 软件编程 m.aspxhome.com