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

Spring实现上拉刷新和下拉加载效果

程序员文章站 2024-02-25 09:06:52
本文实例为大家分享了spring实现上拉刷新和下拉加载效果,供大家参考,具体内容如下 导依赖: compile 'com.android.support:rec...

本文实例为大家分享了spring实现上拉刷新和下拉加载效果,供大家参考,具体内容如下

导依赖:

compile 'com.android.support:recyclerview-v7:25.3.1' 
compile 'com.liaoinstan.springview:library:1.3.0' 
compile files('libs/glide-3.7.0.jar') 

activity_main布局

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
   android:orientation="vertical" 
  tools:context="muhanxi.recycleview.mainactivity"> 
 
 
  <com.liaoinstan.springview.widget.springview 
    android:layout_width="match_parent" 
    android:id="@+id/springview" 
    android:layout_height="match_parent"> 
 
 
    <android.support.v7.widget.recyclerview 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/recyclerview"/> 
 
  </com.liaoinstan.springview.widget.springview> 
 
 
</linearlayout> 

mainactivity 主类 

public class mainactivity extends activity { 
 
  private recyclerview recyclerview; 
  private list<string> list = new arraylist<>(); 
  private springview springview; 
  private linearlayoutmanager linearlayoutmanager; 
 
  @requiresapi(api = build.version_codes.gingerbread) 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
 
    recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); 
 
 
    list.add("http://f10.baidu.com/it/u=2881303562,336932824&fm=72"); 
    list.add("http://f11.baidu.com/it/u=681755471,2018070071&fm=72"); 
    list.add("http://f10.baidu.com/it/u=960650584,863938083&fm=72"); 
    list.add("http://img0.imgtn.bdimg.com/it/u=783060973,4278100629&fm=27&gp=0.jpg"); 
    list.add("http://img1.imgtn.bdimg.com/it/u=3743124979,3234956668&fm=27&gp=0.jpg"); 
    list.add("http://img4.imgtn.bdimg.com/it/u=3468613159,957707785&fm=27&gp=0.jpg"); 
    list.add("http://img3.imgtn.bdimg.com/it/u=2971205354,485034289&fm=27&gp=0.jpg"); 
 
 
    list.add("http://f10.baidu.com/it/u=2881303562,336932824&fm=72"); 
    list.add("http://f11.baidu.com/it/u=681755471,2018070071&fm=72"); 
    list.add("http://f10.baidu.com/it/u=960650584,863938083&fm=72"); 
    list.add("http://img0.imgtn.bdimg.com/it/u=783060973,4278100629&fm=27&gp=0.jpg"); 
    list.add("http://img1.imgtn.bdimg.com/it/u=3743124979,3234956668&fm=27&gp=0.jpg"); 
    list.add("http://img4.imgtn.bdimg.com/it/u=3468613159,957707785&fm=27&gp=0.jpg"); 
    list.add("http://img3.imgtn.bdimg.com/it/u=2971205354,485034289&fm=27&gp=0.jpg"); 
 
 
 
    // 设置布局管理器 
//    new gridlayoutmanager() 九宫格布局管理器 
//    new linearlayoutmanager() listview 布局管理器 
// 
//    new staggeredgridlayoutmanager() 瀑布流 布局管理器 
 
 
//    recyclerview.setlayoutmanager(new gridlayoutmanager(this,5)); 
 
 
 
//    recyclerview.setlayoutmanager(new staggeredgridlayoutmanager(3, linearlayout.vertical)); 
//    recyclerviewadapter adapter = new recyclerviewadapter(this,list) ; 
 
    multiadapter adapter = new multiadapter(this,list) ; 
 
    linearlayoutmanager = new linearlayoutmanager(this,linearlayoutmanager.vertical,false); 
    recyclerview.setlayoutmanager(linearlayoutmanager); 
    recyclerview.setadapter(adapter); 
// 
//    adapter.setilistener(new recyclerviewadapter.listener() { 
//      @override 
//      public void onclick(view view, int position) { 
//        toast.maketext(mainactivity.this, ""+position, toast.length_short).show(); 
//      } 
// 
//      @override 
//      public void longclick(view view, int position) { 
// 
//      } 
//    }); 
 
    recyclerview.setonscrolllistener(new recyclerview.onscrolllistener() { 
      @override 
      public void onscrollstatechanged(recyclerview recyclerview, int newstate) { 
        super.onscrollstatechanged(recyclerview, newstate); 
 
 
//        linearlayoutmanager.findfirstvisibleitemposition() 
 
 
      } 
 
      @override 
      public void onscrolled(recyclerview recyclerview, int dx, int dy) { 
        super.onscrolled(recyclerview, dx, dy); 
      } 
    }); 
 
//    recyclerview.setoni 
  
 
//    recyclerview.additemdecoration(new horizontaldivideritemdecoration.builder(this) 
//    .color(color.red).build()); 
 
 
    springview = (springview) findviewbyid(r.id.springview); 
 
    springview.setheader(new defaultheader(this)); 
    springview.setfooter(new defaultfooter(this)); 
 
 
    springview.setlistener(new springview.onfreshlistener() { 
      @override 
      public void onrefresh() { 
        //下啦 
 
        springview.onfinishfreshandload(); 
      } 
 
      @override 
      public void onloadmore() { 
// 上啦 
        springview.onfinishfreshandload(); 
 
      } 
    }); 
 
  
//    adapter.notifydatasetchanged(); 
  
  } 
  
} 

下拉布局

<?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="wrap_content" 
  android:gravity="center" 
  android:orientation="vertical"> 
   
   
  <imageview 
    android:src="@mipmap/ic_launcher" 
    android:scaletype="centercrop" 
    android:id="@+id/item_simageview" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" /> 
 
  
</linearlayout> 

刷新布局

<?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="wrap_content" 
  android:gravity="center" 
  android:orientation="vertical"> 
   
   
  <imageview 
    android:src="@mipmap/ic_launcher" 
    android:id="@+id/item_simageview" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" /> 
 
  <textview 
 
    android:background="@color/colorprimary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/item_stextview"/> 
 
 
</linearlayout> 

multiadapter 适配器 

public class multiadapter extends recyclerview.adapter<recyclerview.viewholder> { 
 
 
  private context context; 
  private list<string> list; 
 
 
  public multiadapter(context context, list<string> list) { 
    this.context = context; 
    this.list = list; 
 
  } 
 
  @override 
  public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { 
 
    if (viewtype == 0) { 
 
      view view = layoutinflater.from(context).inflate(r.layout.layout_fitem,parent, false); 
      return new fviewholder(view); 
 
    } else { 
      view view = layoutinflater.from(context).inflate(r.layout.layout_sitem, parent, false); 
 
      return new sviewholder(view); 
 
    } 
 
 
  } 
 
 
  @override 
  public int getitemviewtype(int position) { 
    return position % 2 ; 
  } 
 
  @override 
  public void onbindviewholder(recyclerview.viewholder holder, int position) { 
 
 
 
 
    if(holder instanceof fviewholder){ 
 
      fviewholder fviewholder = (fviewholder)holder ; 
      fviewholder.itemstextview.settext(position+""); 
      glide.with(context).load(list.get(position)).into(fviewholder.itemsimageview); 
 
//      imageloader.getinstance().displayimage(list.get(position),fviewholder.itemsimageview); 
 
 
    } else { 
 
      sviewholder sviewholder = (sviewholder)holder ; 
      glide.with(context).load(list.get(position)).into(sviewholder.itemsimageview); 
 
//      imageloader.getinstance().displayimage(list.get(position),sviewholder.itemsimageview); 
 
    } 
 
 
  } 
 
  @override 
  public int getitemcount() { 
    return list.size(); 
  } 
 
 
 
  static class fviewholder extends recyclerview.viewholder{ 
    @bindview(r.id.item_simageview) 
    imageview itemsimageview; 
    @bindview(r.id.item_stextview) 
    textview itemstextview; 
 
    fviewholder(view view) { 
      super(view); 
      butterknife.bind(this, view); 
    } 
  } 
 
  class sviewholder extends recyclerview.viewholder{ 
    @bindview(r.id.item_simageview) 
    imageview itemsimageview; 
 
    sviewholder(view view) { 
      super(view); 
      butterknife.bind(this, view); 
    } 
  } 
} 

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