欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

RecyclerView实现横向滚动效果

程序员文章站 2022-06-09 19:09:43
本文实例为大家分享了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);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。