Android listview實現分組
阿新 • • 發佈:2019-02-12
對於Listview的分組我們再熟悉不過了,因為Android自帶的通訊錄中的聯絡人資訊就是使用的ListView分組,最近專案中用到了這個功能。所以趁著週末有時間,也更新下一篇這樣的部落格,希望對大家能夠有幫助。
因為listview要載入兩種不同的item,所以要實現兩個item佈局,addexam_list_item.xml:
分組標籤對應的佈局addexam_list_item_tag.xml
佈局檔案我們已經實現了,下面看下在程式中我們是怎麼處理的吧!
程式碼好像挺簡單,更我們平時使用lsitview也沒多大區別,下面看看能不能實現呢
其實對於分組的ListView和我們平時用的ListView沒有多大差別,就是需要在介面卡中的getView方法中做下判斷。只要理解了這個,下面就好說了,下面我們看下實現程式碼。
首先是main.xml佈局:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- android:orientation="vertical">
- <ListView
- android:id="@+id/listView_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </
- </LinearLayout>
因為listview要載入兩種不同的item,所以要實現兩個item佈局,addexam_list_item.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation
- android:padding="5dip">
- <ImageView
- android:id="@+id/addexam_list_icon"
- android:background="@drawable/ic_launcher"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/addexam_list_item_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginLeft="10dp"
- android:text="測試資料"/>
- </LinearLayout>
分組標籤對應的佈局addexam_list_item_tag.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#666666"
- android:paddingLeft="10dp"
- android:gravity="center_vertical"
- android:orientation="vertical">
- <TextView
- android:id="@+id/addexam_list_item_text"
- android:layout_width="wrap_content"
- android:layout_height="20dip"
- android:textColor="#ffffff"
- android:text="金融考試"
- android:gravity="center_vertical"/>
- </LinearLayout>
佈局檔案我們已經實現了,下面看下在程式中我們是怎麼處理的吧!
- public class TestActivity extends Activity {
- /** Called when the activity is first created. */
- private List<String>list=null;
- private List<String>groupkey=new ArrayList<String>();
- private List<String>aList = new ArrayList<String>();
- private List<String>bList = new ArrayList<String>();
- private ListView listview;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listview=(ListView) findViewById(R.id.listView_list);
- initData();
- MyAdapter adapter=new MyAdapter();
- listview.setAdapter(adapter);
- }
- public void initData(){
- list = new ArrayList<String>();
- groupkey.add("A組");
- groupkey.add("B組");
- for(int i=0; i<5; i++){
- aList.add("A組"+i);
- }
- list.add("A組");
- list.addAll(aList);
- for(int i=0; i<8; i++){
- bList.add("B組"+i);
- }
- list.add("B組");
- list.addAll(bList);
- }
- private class MyAdapter extends BaseAdapter{
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return list.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public boolean isEnabled(int position) {
- // TODO Auto-generated method stub
- if(groupkey.contains(getItem(position))){
- return false;
- }
- return super.isEnabled(position);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- View view=convertView;
- if(groupkey.contains(getItem(position))){
- view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null);
- }else{
- view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null);
- }
- TextView text=(TextView) view.findViewById(R.id.addexam_list_item_text);
- text.setText((CharSequence) getItem(position));
- return view;
- }
- }
- }
程式碼好像挺簡單,更我們平時使用lsitview也沒多大區別,下面看看能不能實現呢
執行一下:
轉自http://blog.csdn.net/wangkuifeng0118/article/details/7460142