Android ListView Add header layout and foot layout examples detailed _android

Source: Internet
Author: User
Tags static class

Android ListView Add header layout and foot layout

Before learning Himalaya A small demo, posted out for everyone to learn the reference;

If our current page has more than one interface, a variety of layouts, our general choice is nothing more than 1, multiple layout, 2, a variety of complex sliding layout outside a layer of scrollview (good low); 3, head layout foot layout. Sometimes we use a lot of layout is not very good to achieve, so the head layout is our best choice of the layout of the ListView, learned the words of the principle is very simple, no understanding of things, directly affixed to the code:

Effect Chart:

Body Part layout:

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" &

  Gt <view android:id= "@+id/teach_classify_item_divider" android:background= "#f3fdeeee" Android:layout_width= "Ma Tch_parent "android:layout_height=" "10DP"/> <linearlayout android:layout_width= "Match_parent" Android: layout_height= "match_parent" android:orientation= "horizontal" > <relativelayout android:id= "@+id/teac" H_classify_left "android:layout_width=" 0DP "android:background=" @drawable/item_pressed "Android:layout_h
        eight= "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_classi fy_item_text01 "android:layout_width=" match_parent "android:layout_height=" 45DP "Android:layout_ce Ntervertical= "true" android:layout_marginleft= "60DP" android:gravity= "center_vertical" Android:tex t= "@string/app_name"/> </RelativeLayout> <view android:layout_width= "1DP" android:layou t_height= "Match_parent" android:background= "#efe6e6"/> <relativelayout android:id= "@+id/teach_clas Sify_right "android:layout_width=" 0DP "android:background=" @drawable/item_pressed "android:layout_height = "Match_parent" android:layout_marginleft= "15DP" android:layout_weight= "1" > <imageview an
        Droid: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_classif y_item_text02 "android:layout_width=" match_parent "android:layout_height=" 45DP "Android:layout_cen Tervertical= "true" android:layout_marginleft= "60DP" android:gravity= "center_vertical" Android:text

 = "@string/app_name"/> </RelativeLayout> </LinearLayout> </LinearLayout>

Header layout:

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>

Foot layout:

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"
    @ Mipmap/ic_launcher "
    android:layout_width=" match_parent "
    android:layout_height=" 160DP "/>
</ Linearlayout> 

Main Page:

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 (savedins
    Tancestate);
    Initview ();
  Setupview (); /** * Header Add is best placed before Setadapter, before Android4.4, header add must be placed before setting adapter * * private void Initview () {MLISTVI
    ew = ((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));
    Multiple Headerview Mlistview.addheaderview (Headerview) can be added;
    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); /** * Network request/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&Gt
        List = Classifylist.getlist ();
        Update adapter adapter.updateres (list);
      Update 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);

 }
}

Adapter:

public class Classifyadapter extends Baseadapter implements View.onclicklistener {private static final String TAG = C
  LassifyAdapter.class.getSimpleName ();
  Private list<classify> data;
  Private Layoutinflater Inflater; Private Onclickitemlistener listener;//hold interface public void Setlistener (Onclickitemlistener listener) {this.listener=
  Listener Classifyadapter (context Context,list<classify>data) {inflater= (layoutinflater) Context.getsystemse
    Rvice (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 ();
    /////According to the conditions to see if the split line is displayed if (position%3==0&&position!=0) {holder.topDivider.setVisibility (view.visible);
    }else {holder.topDivider.setVisibility (view.gone);
    }//Load Data Holder.itemText01.setText (Data.get (position*2+1). GetTitle ());
    Holder.itemText02.setText (Data.get (position*2+2). GetTitle ());
    Set up listening Holder.leftItem.setOnClickListener (this);
    Holder.rightItem.setOnClickListener (this);
    Set tag Holder.leftItem.setTag (position*2+1);
    Holder.rightItem.setTag (position*2+2);
    Load Picture 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{//left picture ImageView itemIamge01;
    The picture on the right ImageView itemImage02;
    Right TextView itemText01;

    TextView itemText02;
    Split Line View Topdivider;
  Left and right layout View Leftitem,rightitem;
  public interface onclickitemlistener{void Ononclickitem (int position);

 }
}

Model class:

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
  }
}

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.