写点什么

Android PinnedHeaderListView 详解,flutter 技术解析与实战

发布于: 23 小时前

(一) 使用:



xml 布局 ?

<?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:orientation="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) {


super.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, menu);


return true;


}


}

? adapter

继承 ? ?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


return 0;


}


@Override


public int getSectionCount() {//header 的数量


return 3;


}


@Override


public int getCountForSection(int section) {//子 item 的数量


return 15;


}


@Override


public View getItemView(int section, int position, View convertView, ViewGroup parent) {//普通 item 的 getview 方法


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 方法


LinearLayout layout = null;


if (convertView == null) {


LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.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 section " + section);


return layout;


}


}


(二)解析:




要使用它 ?需要继承 ? ?SectionedBaseAdapter ?


重写 adapter 之中的 6 个方法


  • 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


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


position, View convertView, ViewGroup parent);`


  • public View getSectionHeaderView(int section, View convertView, ViewGroup parent);


getItem(int section, int position); ?//section: header 的位置数 ?,position:普通 item 的位置数


getItemId(int section, int position); ?//获取 item 的 id ?第几个 section 的 position?


getSectionCount(); ?//header 的总数量


getCountForSection(int section); ? ?//普通 item 的总数量

评论

发布
暂无评论
Android PinnedHeaderListView 详解,flutter技术解析与实战