ExpandableListView小结

Java代码:
 package com.example.expandablelistviewdemo;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;

/**
 * Example application with ExpandableListView and CheckedTextView as list item.
 * Texts of selected list items are displayed in parent view.
 * 
 * @author Lauri Nevala
 * 
 *
 */
public class MainActivity extends Activity {
private SettingsListAdapter adapter;
private ExpandableListView categoriesList;
private ArrayList categories;
protected Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
categoriesList = (ExpandableListView)findViewById(R.id.categories);
categories = Category.getCategories();
adapter = new SettingsListAdapter(this, 
categories, categoriesList);
        categoriesList.setAdapter(adapter);
        categoriesList.expandGroup(0);
        
        categoriesList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

CheckedTextView checkbox = (CheckedTextView)v.findViewById(R.id.list_item_text_child);
checkbox.toggle();
// find parent view by tag
View parentView = categoriesList.findViewWithTag(categories.get(groupPosition).name);
if(parentView != null) {
TextView sub = (TextView)parentView.findViewById(R.id.list_item_text_subscriptions);
if(sub != null) {
Category category = categories.get(groupPosition);
if(checkbox.isChecked()) {
// add child category to parent's selection list
category.selection.add(checkbox.getText().toString());
// sort list in alphabetical order
Collections.sort(category.selection, new CustomComparator());
}
else {
// remove child category from parent's selection list
category.selection.remove(checkbox.getText().toString());
}
// display selection list
sub.setText(category.selection.toString());
}
}
return true;
}
});
}
public class CustomComparator implements Comparator {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
            
        }
    }
    

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



activity_main.xml布局代码:



   categoriesList.expandGroup(0);






setting_list_item_child.xml代码:


 

 
    

 
 settings_list_item_parent.xml代码:

 


 



 

  

         显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,但界面优化方面做得还不够好,有待改进。 下面直接上效果图以及源代码如上~!    


ExpandableListView小结_第1张图片





你可能感兴趣的:(技术分享)