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

Android RecycleView添加head配置封装的实例

程序员文章站 2023-12-18 13:05:52
android recycleview添加head配置封装的实例 这个是把recycleview的适配器给封装了,直接调用就可以了,还添加了可以添加head头部功能,很赞...

android recycleview添加head配置封装的实例

这个是把recycleview的适配器给封装了,直接调用就可以了,还添加了可以添加head头部功能,很赞的,今天记下来,下次直接用 

实例代码:

package com.wwl.android; 
 
import android.support.v7.widget.gridlayoutmanager; 
import android.support.v7.widget.recyclerview; 
import android.support.v7.widget.staggeredgridlayoutmanager; 
import android.view.view; 
import android.view.viewgroup; 
 
import java.util.arraylist; 
 
 
public abstract class baserecycleradapter<t> extends recyclerview.adapter<recyclerview.viewholder> { 
 
  public static final int type_header = 0; 
  public static final int type_normal = 1; 
 
  private arraylist<t> mdatas = new arraylist<>(); 
 
  private view mheaderview; 
 
  private onitemclicklistener mlistener; 
 
  public void setonitemclicklistener(onitemclicklistener li) { 
    mlistener = li; 
  } 
 
  public void setheaderview(view headerview) { 
    mheaderview = headerview; 
    notifyiteminserted(0); 
  } 
 
  public view getheaderview() { 
    return mheaderview; 
  } 
 
  public void adddatas(arraylist<t> datas) { 
    mdatas.addall(datas); 
    notifydatasetchanged(); 
  } 
 
  @override 
  public int getitemviewtype(int position) { 
    if(mheaderview == null) return type_normal; 
    if(position == 0) return type_header; 
    return type_normal; 
  } 
 
  @override 
  public recyclerview.viewholder oncreateviewholder(viewgroup parent, final int viewtype) { 
    if(mheaderview != null && viewtype == type_header) return new holder(mheaderview); 
    return oncreate(parent, viewtype); 
  } 
 
 
  @override 
  public void onbindviewholder(recyclerview.viewholder viewholder, int position) { 
    if(getitemviewtype(position) == type_header) return; 
 
    final int pos = getrealposition(viewholder); 
    final t data = mdatas.get(pos); 
    onbind(viewholder, pos, data); 
 
    if(mlistener != null) { 
      viewholder.itemview.setonclicklistener(new view.onclicklistener() { 
        @override 
        public void onclick(view v) { 
          mlistener.onitemclick(pos, data); 
        } 
      }); 
    } 
  } 
 
  @override 
  public void onattachedtorecyclerview(recyclerview recyclerview) { 
    super.onattachedtorecyclerview(recyclerview); 
 
    recyclerview.layoutmanager manager = recyclerview.getlayoutmanager(); 
    if(manager instanceof gridlayoutmanager) { 
      final gridlayoutmanager gridmanager = ((gridlayoutmanager) manager); 
      gridmanager.setspansizelookup(new gridlayoutmanager.spansizelookup() { 
        @override 
        public int getspansize(int position) { 
          return getitemviewtype(position) == type_header 
              ? gridmanager.getspancount() : 1; 
        } 
      }); 
    } 
  } 
 
  @override 
  public void onviewattachedtowindow(recyclerview.viewholder holder) { 
    super.onviewattachedtowindow(holder); 
    viewgroup.layoutparams lp = holder.itemview.getlayoutparams(); 
    if(lp != null 
        && lp instanceof staggeredgridlayoutmanager.layoutparams 
        && holder.getlayoutposition() == 0) { 
      staggeredgridlayoutmanager.layoutparams p = (staggeredgridlayoutmanager.layoutparams) lp; 
      p.setfullspan(true); 
    } 
  } 
 
  public int getrealposition(recyclerview.viewholder holder) { 
    int position = holder.getlayoutposition(); 
    return mheaderview == null ? position : position - 1; 
  } 
 
  @override 
  public int getitemcount() { 
    return mheaderview == null ? mdatas.size() : mdatas.size() + 1; 
  } 
 
  public abstract recyclerview.viewholder oncreate(viewgroup parent, final int viewtype); 
  public abstract void onbind(recyclerview.viewholder viewholder, int realposition, t data); 
 
  public class holder extends recyclerview.viewholder { 
    public holder(view itemview) { 
      super(itemview); 
    } 
  } 
 
  public interface onitemclicklistener<t> { 
    void onitemclick(int position, t data); 
  } 
} 

  然后在外面自己的适配器中直接继承就可以了

package com.wwl.android; 
 
import android.support.v7.widget.recyclerview; 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.textview; 
 
/** 
 * wwl 
 */ 
public class myadapter extends baserecycleradapter<string> { 
 
  @override 
  public recyclerview.viewholder oncreate(viewgroup parent, int viewtype) { 
    view layout = layoutinflater.from(parent.getcontext()).inflate(r.layout.item, parent, false); 
    return new myholder(layout); 
  } 
 
  @override 
  public void onbind(recyclerview.viewholder viewholder, int realposition, string data) { 
    if(viewholder instanceof myholder) { 
      ((myholder) viewholder).text.settext(data); 
    } 
  } 
 
  class myholder extends baserecycleradapter.holder { 
    textview text; 
    public myholder(view itemview) { 
      super(itemview); 
      text = (textview) itemview.findviewbyid(r.id.text); 
    } 
  } 
} 
 

 然后在类中调用,

public class mainactivity extends appcompatactivity { 
 
  private recyclerview mrecyclerview; 
  private recyclerview.layoutmanager mlayoutmanager; 
  private myadapter madapter; 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
 
    mrecyclerview = (recyclerview) findviewbyid(r.id.list); 
//    mlayoutmanager = new linearlayoutmanager(this, linearlayoutmanager.vertical, false); 
    mlayoutmanager = new gridlayoutmanager(this, 2); 
//    mlayoutmanager = new staggeredgridlayoutmanager(2, staggeredgridlayoutmanager.vertical); 
    mrecyclerview.setlayoutmanager(mlayoutmanager); 
    mrecyclerview.setitemanimator(new defaultitemanimator()); 
    mrecyclerview.additemdecoration(new griditemdecoration(this, true)); 
 
    madapter = new myadapter(); 
    mrecyclerview.setadapter(madapter); 
    madapter.adddatas(generatedata()); 
    setheader(mrecyclerview); 
    madapter.setonitemclicklistener(new baserecycleradapter.onitemclicklistener<string>() { 
      @override 
      public void onitemclick(int position, string data) { 
        toast.maketext(mainactivity.this, position + "," + data, toast.length_short).show(); 
      } 
    }); 
  } 
 
  private void setheader(recyclerview view) { 
    view header = layoutinflater.from(this).inflate(r.layout.header, view, false); 
     
    madapter.setheaderview(header); 
  } 
 
  private arraylist<string> generatedata() { 
    arraylist<string> data = new arraylist<string>() { 
      { 
        for(int i=0;i<21;i++) add("数据" + i); 
      } 
    }; 
    return data; 
  } 
   
} 

 这样就ok啦。 

布局文件:

r.layout.activity_main 

<?xml version="1.0" encoding="utf-8"?> 
<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"> 
 
  <android.support.v7.widget.recyclerview 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
 
</linearlayout> 

header.xml 

<?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:orientation="vertical"> 
 
  <imageview 
    android:layout_gravity="center_horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher"/> 
 
</linearlayout> 

   item.xml 

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:padding="20dp" 
  android:orientation="vertical"> 
 
  <textview 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerinparent="true" 
    android:text="数据"/> 
 
</relativelayout> 

以上就是android recycleview添加head配置封装的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: