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

RecyclerView的简单使用

程序员文章站 2023-12-17 08:12:46
recyclerview的功能很强大,本文为大家分享recyclerview的简单使用方法。 效果图: activity_main.xml: <...

recyclerview的功能很强大,本文为大家分享recyclerview的简单使用方法。

效果图:

RecyclerView的简单使用

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.v7.widget.recyclerview
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

</linearlayout>

fruit_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="5dp">

 <imageview
  android:id="@+id/fruit_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"/>

 <textview
  android:id="@+id/fruit_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="left"
  android:layout_margintop="10dp"/>

</linearlayout>

fruit实体类:

public class fruit {

 private string name;
 private int imageid;

 public fruit(string name, int imageid) {
  this.name = name;
  this.imageid = imageid;
 }

 public string getname() {
  return name;
 }

 public void setname(string name) {
  this.name = name;
 }

 public int getimageid() {
  return imageid;
 }

 public void setimageid(int imageid) {
  this.imageid = imageid;
 }
}

fruitadapter:

public class fruitadapter extends recyclerview.adapter<fruitadapter.viewholder> {

 private list<fruit> mfruitlist;

 static class viewholder extends recyclerview.viewholder{
  imageview fruitimage;
  textview fruitname;
  public viewholder(view view){
   super(view);
   fruitimage = view.findviewbyid(r.id.fruit_image);
   fruitname = view.findviewbyid(r.id.fruit_name);
  }
 }

 public fruitadapter(list<fruit> fruitlist){
  mfruitlist = fruitlist;
 }

 @override
 public viewholder oncreateviewholder(viewgroup parent, int viewtype) {
  view view = layoutinflater.from(parent.getcontext()).inflate(
    r.layout.fruit_item,parent,false);
  final viewholder holder = new viewholder(view);
  holder.fruitimage.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
    fruit fruit = mfruitlist.get(holder.getadapterposition());
    toast.maketext(view.getcontext(),"you clicked view "+fruit.getname(),toast.length_short).show();
   }
  });
  return holder;
 }

 @override
 public void onbindviewholder(viewholder holder, int position) {
  fruit fruit = mfruitlist.get(position);
  holder.fruitimage.setimageresource(fruit.getimageid());
  holder.fruitname.settext(fruit.getname());
 }

 @override
 public int getitemcount() {
  return mfruitlist.size();
 }

}

mainactivity:

public class mainactivity extends appcompatactivity {

 list<fruit> fruitlist = new arraylist<>();

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initfruit();
  recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recycler_view);
  staggeredgridlayoutmanager manager = new staggeredgridlayoutmanager(3,
    staggeredgridlayoutmanager.vertical);
  recyclerview.setlayoutmanager(manager);
  fruitadapter adapter = new fruitadapter(fruitlist);
  recyclerview.setadapter(adapter);
 }

 public void initfruit(){
  for (int i = 0; i < 2; i++) {
   fruit apple = new fruit(getrandomlengthname("apple"), r.drawable.apple_pic);
   fruitlist.add(apple);
   fruit banana = new fruit(getrandomlengthname("banana"), r.drawable.banana_pic);
   fruitlist.add(banana);
   fruit orange = new fruit(getrandomlengthname("orange"), r.drawable.orange_pic);
   fruitlist.add(orange);
   fruit watermelon = new fruit(getrandomlengthname("watermelon"), r.drawable.watermelon_pic);
   fruitlist.add(watermelon);
   fruit pear = new fruit(getrandomlengthname("pear"), r.drawable.pear_pic);
   fruitlist.add(pear);
   fruit grape = new fruit(getrandomlengthname("grape"), r.drawable.grape_pic);
   fruitlist.add(grape);
   fruit pineapple = new fruit(getrandomlengthname("pineapple"), r.drawable.pineapple_pic);
   fruitlist.add(pineapple);
   fruit strawberry = new fruit(getrandomlengthname("strawberry"), r.drawable.strawberry_pic);
   fruitlist.add(strawberry);
   fruit cherry = new fruit(getrandomlengthname("cherry"), r.drawable.cherry_pic);
   fruitlist.add(cherry);
   fruit mango = new fruit(getrandomlengthname("mango"), r.drawable.mango_pic);
   fruitlist.add(mango);
  }
 }


 public string getrandomlengthname(string name){
  random random = new random();
  int length = random.nextint(20)+1;
  stringbuilder builder = new stringbuilder();
  for(int i = 0; i < length; i++){
   builder.append(name);
  }
  return builder.tostring();
 }

}

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

上一篇:

下一篇: