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

Android ListView添加头布局和脚布局实例详解

程序员文章站 2024-02-29 23:44:10
android listview添加头布局和脚布局 之前学习喜马拉雅的时候做的一个小demo,贴出来,供大家学习参考; 如果我们当前的页面有多个接口、多种布局的话,...

android listview添加头布局和脚布局

之前学习喜马拉雅的时候做的一个小demo,贴出来,供大家学习参考;

如果我们当前的页面有多个接口、多种布局的话,我们一般的选择无非就是1、多布局;2、各种复杂滑动布局外面套一层scrollview(好low);3、头布局脚布局。有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了;学过了listview的话原理很简单,没啥理解的东西,直接贴代码了:

效果图:

                 Android ListView添加头布局和脚布局实例详解Android ListView添加头布局和脚布局实例详解

正文部分布局:

fragment_classify.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">
  <listview
    android:id="@+id/teach_classify_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:listselector="#00000000"/>
</linearlayout>

classify_item.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="match_parent"
  android:orientation="vertical">

  <view
    android:id="@+id/teach_classify_item_divider"
    android:background="#f3fdeeee"
    android:layout_width="match_parent"
    android:layout_height="10dp"/>

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <relativelayout
      android:id="@+id/teach_classify_left"
      android:layout_width="0dp"
      android:background="@drawable/item_pressed"
      android:layout_height="match_parent"
      android:layout_marginleft="15dp"
      android:layout_weight="1">

      <imageview
        android:id="@+id/teach_classify_item_iamge01"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centervertical="true"
        android:src="@mipmap/ic_launcher" />

      <textview
        android:id="@+id/teach_classify_item_text01"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_centervertical="true"
        android:layout_marginleft="60dp"
        android:gravity="center_vertical"
        android:text="@string/app_name" />

    </relativelayout>

    <view
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="#efe6e6" />

    <relativelayout
      android:id="@+id/teach_classify_right"
      android:layout_width="0dp"
      android:background="@drawable/item_pressed"
      android:layout_height="match_parent"
      android:layout_marginleft="15dp"
      android:layout_weight="1">

      <imageview
        android:id="@+id/teach_classify_item_iamge02"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centervertical="true"
        android:src="@mipmap/ic_launcher" />

      <textview
        android:id="@+id/teach_classify_item_text02"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_centervertical="true"
        android:layout_marginleft="60dp"
        android:gravity="center_vertical"
        android:text="@string/app_name" />

    </relativelayout>
  </linearlayout>

</linearlayout>

头布局:

fragment_classify_header.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">
  <imageview
    android:id="@+id/teach_classify_lv_header"
    android:src="@mipmap/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="180dp" />
</linearlayout>

脚布局:

fragment_classify_bottom.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">
  <imageview
    android:id="@+id/teach_classify_bottom"
    android:src="@mipmap/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="160dp" />
</linearlayout> 

主页面:

public class classifyfragment extends basefragment implements classifyadapter.onclickitemlistener{
  public static final string tag = classifyfragment.class.getsimplename();
  private listview mlistview;
  private classifyadapter adapter;
  private imageview mheaderimage;
  private imageview mbottomimage;

  @nullable
  @override
  public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
    layout = inflater.inflate(r.layout.fragment_classify, container, false);
    return layout;
  }

  @override
  public void onactivitycreated(@nullable bundle savedinstancestate) {
    super.onactivitycreated(savedinstancestate);
    initview();
    setupview();
  }
  /**
   * header的添加最好放在setadapter之前,在android4.4之前,header添加必须放在设置adapter之前
   */
  private void initview() {
    mlistview = ((listview) layout.findviewbyid(r.id.teach_classify_listview));
    //header
    view headerview = layoutinflater.from(getactivity()).inflate(r.layout.fragment_classify_header,null);
    mheaderimage = ((imageview) headerview.findviewbyid(r.id.teach_classify_lv_header));
    //可以添加多个headerview
    mlistview.addheaderview(headerview);
    //bottom
    view bottomview = layoutinflater.from(getactivity()).inflate(r.layout.fragment_classify_bottom,null);
    mbottomimage = ((imageview) bottomview.findviewbyid(r.id.teach_classify_bottom));
    mlistview.addfooterview(bottomview);
    adapter = new classifyadapter(getactivity(), null);
    mlistview.setadapter(adapter);
  }

  /**
   * 网络请求
   */
  private void setupview() {
    httputil.getstringasync(httpconstant.classify_url, new httputil.requestcallback() {
      @override
      public void onfailure() {
        log.e(tag, "onfailure: ");
      }

      @override
      public void onsuccess(string result) {
        log.e(tag, "onsuccess: " + result);
        gson gson = new gson();
        classifylist classifylist = gson.fromjson(result, classifylist.class);
        list<classify> list = classifylist.getlist();
        //更新适配器
        adapter.updateres(list);
        //更新header
        imageloader.display(mheaderimage,list.get(0).getcoverpath());
      }

      @override
      public void onfinish() {
        log.e(tag, "onfinish: ");
      }
    });
    string url_bottom="http://adse.ximalaya.com/ting?device=android&name=cata_index_banner&network=wifi&operator=0&version=4.3.98";
    httputil.getstringasync(url_bottom, new httputil.requestcallback() {
      @override
      public void onfailure() {
      }

      @override
      public void onsuccess(string result) {
        gson gson = new gson();
        classifybottomlist classifybottomlist = gson.fromjson(result, classifybottomlist.class);
        imageloader.display(mbottomimage, classifybottomlist.getdata().get(0).getcover());
      }

      @override
      public void onfinish() {
      }
    });
  }
  @override
  public void ononclickitem(int position) {
    log.e(tag, "ononclickitem:------------- "+position );
  }
}

适配器:

public class classifyadapter extends baseadapter implements view.onclicklistener {

  private static final string tag = classifyadapter.class.getsimplename();
  private list<classify> data;
  private layoutinflater inflater;
  private onclickitemlistener listener;//持有接口

  public void setlistener(onclickitemlistener listener){
      this.listener=listener;
  }

  public classifyadapter(context context,list<classify>data) {
    inflater= (layoutinflater) context.getsystemservice(context.layout_inflater_service);
    if (data!=null) {
      this.data=data;
    }
    else {
      this.data=new arraylist<>();
    }
  }

  public void updateres(list<classify> data){
    if (data!=null) {
      this.data.clear();
      this.data.addall(data);
      notifydatasetchanged();
    }
  }

  @override
  public int getcount() {
    int count=0;
    if (data!=null) {
      count=(data.size()-1)/2;
    }
    return count;
  }

  @override
  public classify getitem(int position) {
    return data.get(position);
  }

  @override
  public long getitemid(int position) {
    return position;
  }

  @override
  public view getview(int position, view convertview, viewgroup parent) {
    viewholder holder=null;
    if (convertview==null) {
      convertview=inflater.inflate(r.layout.classify_item,parent,false);
      holder=new viewholder();
      holder.itemiamge01= (imageview) convertview.findviewbyid(r.id.teach_classify_item_iamge01);
      holder.itemimage02= (imageview) convertview.findviewbyid(r.id.teach_classify_item_iamge02);
      holder.itemtext01= (textview) convertview.findviewbyid(r.id.teach_classify_item_text01);
      holder.itemtext02= (textview) convertview.findviewbyid(r.id.teach_classify_item_text02);
      holder.topdivider=convertview.findviewbyid(r.id.teach_classify_item_divider);
      holder.leftitem=convertview.findviewbyid(r.id.teach_classify_left);
      holder.rightitem=convertview.findviewbyid(r.id.teach_classify_right);
      convertview.settag(holder);
    }
    else {
      holder= (viewholder) convertview.gettag();
    }
    //根据条件判断是否显示分割线
    if (position%3==0&&position!=0) {
      holder.topdivider.setvisibility(view.visible);
    }else {
      holder.topdivider.setvisibility(view.gone);
    }
    //加载数据
    holder.itemtext01.settext(data.get(position*2+1).gettitle());
    holder.itemtext02.settext(data.get(position*2+2).gettitle());
    //设置监听
    holder.leftitem.setonclicklistener(this);
    holder.rightitem.setonclicklistener(this);
    //设置标记
    holder.leftitem.settag(position*2+1);
    holder.rightitem.settag(position*2+2);
    //加载图片
    imageloader.display(holder.itemiamge01,data.get(position*2+1).getcoverpath());
    imageloader.display(holder.itemimage02,data.get(position*2+2).getcoverpath());
    return convertview;
  }

  @override
  public void onclick(view v) {
    integer position = (integer) v.gettag();
    log.e(tag, "onclick: "+position );
    if (listener!=null) {
      listener.ononclickitem(position);
    }

  }

  private static class viewholder{
    //左边的图片
    imageview itemiamge01;
    //右边的图片
    imageview itemimage02;
    //右边
    textview itemtext01;
    textview itemtext02;

    //分割线
    view topdivider;
    //左右布局
    view leftitem,rightitem;
  }

  public interface onclickitemlistener{
    void ononclickitem(int position);
  }
}

model类:

public class classify {

  private string title;
  private string coverpath;

  public string gettitle() {
    return title;
  }

  public void settitle(string title) {
    this.title = title;
  }

  public string getcoverpath() {
    return coverpath;
  }

  public void setcoverpath(string coverpath) {
    this.coverpath = coverpath;
  }
}

public class classifylist {
  private list<classify> list;

  public list<classify> getlist() {
    return list;
  }

  public void setlist(list<classify> list) {
    this.list = list;
  }
}

public class classifybottom {
  private string cover;

  public string getcover() {
    return cover;
  }

  public void setcover(string cover) {
    this.cover = cover;
  }
}

public class classifybottomlist {
  private list<classifybottom> data;

  public list<classifybottom> getdata() {
    return data;
  }

  public void setdata(list<classifybottom> data) {
    this.data = data;
  }
}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!