Android Pinnedheaderlistview Use detailed
Pinnedheaderlistview provides some staples that will be set in the head
It looks like the app on Android 4.0 more than the contacts can smooth the hover
GitHub Open Source address: Https://github.com/JimiSmith/PinnedHeaderListView
:
Libray only 2 classes in the Import editing tool after download
Pinnedheaderlistview.java
Sectionedbaseadapter.java
(i) Use:XML Layout
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <za.co.immedia.pinnedheaderlistview.pinnedheaderlistview android:id= "@+id /pinnedlistview " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: Background= "@android: Color/white" > </za.co.immedia.pinnedheaderlistview.PinnedHeaderListView>< /linearlayout>
Activity
public class Mainactivity extends Activity {@Override public void onCreate (Bundle savedinstancestate) {Supe R.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Pinnedheaderlistview ListView = (pinnedheaderlistview) Findviewbyid (R.id.pinnedlistview); Layoutinflater inflator = (layoutinflater) getsystemservice (Context.layout_inflater_service); LinearLayout header1 = (linearlayout) inflator.inflate (R.layout.list_item, NULL); ((TextView) Header1.findviewbyid (R.id.textitem)). SetText ("HEADER 1"); LinearLayout Header2 = (linearlayout) inflator.inflate (R.layout.list_item, NULL); ((TextView) Header2.findviewbyid (R.id.textitem)). SetText ("HEADER 2"); LinearLayout footer = (linearlayout) inflator.inflate (R.layout.list_item, NULL); ((TextView) Footer.findviewbyid (R.id.textitem)). SetText ("footer"); Listview.addheaderview (Header1); Listview.addheaderview (Header2); Listview.addfooterviEW (footer); Testsectionedadapter sectionedadapter = new Testsectionedadapter (); Listview.setadapter (Sectionedadapter); @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_main, M ENU); return true; }}
AdapterInherit Sectionedbaseadapter
Package Za.co.immedia.pinnedheaderlistviewexample;import Za.co.immedia.pinnedheaderlistview.SectionedBaseAdapter ; Import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.linearlayout;import Android.widget.textview;public Class Testsectionedadapter extends Sectionedbaseadapter {@Override public Object getItem (int section, int position) { TODO auto-generated method stub return null; } @Override public long getitemid (int section, int position) {//TODO auto-generated method stub Retu RN 0; } @Override the number of public int getsectioncount () {//header return 3; } @Override public int getcountforsection (int section) {//Sub-item number return 15; } @Override public View getitemview (int section, int position, View Convertview, ViewGroup parent) {//plain item GetView Method LinearLayout layout = null; if (Convertview = = null) { Layoutinflater inflator = (layoutinflater) parent.getcontext (). Getsystemservice (Context.layout_inflater_service); Layout = (linearlayout) inflator.inflate (R.layout.list_item, NULL); } else {layout = (linearlayout) Convertview; } ((TextView) Layout.findviewbyid (R.id.textitem)). SetText ("section" + section + "Item" + position); return layout; } @Override public View getsectionheaderview (int section, View Convertview, ViewGroup parent) {//header GetView method LinearLayout layout = null; if (Convertview = = null) {Layoutinflater inflator = (layoutinflater) parent.getcontext (). Getsystemservice (Cont Ext. Layout_inflater_service); Layout = (linearlayout) inflator.inflate (R.layout.header_item, NULL); } else {layout = (linearlayout) Convertview; } ((TextView) Layout.findviewbyid (R.id.textitem)). SetText ("Header for Sections" + section); return layout; }}
(b) Analysis:
To use it, you need to inherit Sectionedbaseadapter
6 ways to rewrite adapter
public Object getItem(int section, int position);
public long getItemId(int section, int position);
public int getSectionCount();
public int getCountForSection(int section);
public View getItemView(int section, int position, View convertView, ViewGroup parent);
public View getSectionHeaderView(int section, View convertView, ViewGroup parent);
GetItem (int section, int position); Section:header number of positions,position: number of positions for normal item
Getitemid (int section, int position); Gets the ID of item number section of position
Getsectioncount (); Total number of//header
getcountforsection (int section); //Total number of ordinary item
Getitemview (int section, int position, View Convertview, viewgroup parent);
Normal item Load view method similar to adapter in GetView () method
getsectionheaderview (int section, View Convertview, viewgroup parent);
Header Item Loads the view method also similar to adapter in the GetView () method
You can use HashMap to save the list when you use it to display data through sections and position.
The section can also be judged separately to show the different headers
Last God Beast Town Building
┏┓┏┓//┏┛┻━━━┛┻┓//┃┃//┃━┃//┃┳┛┗┳┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛// ┃┃ god beast bless ┃┃ Code no bug! ┃┗━━━┓// ┃┣┓// ┃┏┛// ┗┓┓┏━┳┓┏┛// ┃┫┫┃┫┫// ┗┻┛┗┻┛
Reprint Please specify: http://blog.csdn.net/aaawqqq/article/details/43866339
Wishing you a happy New year.
Android Pinnedheaderlistview Detailed