Android使用ExpandableListView实现折叠的效果

主要用过一个ExpandableListView+自定义一个继承BaseExpandableListAdapter的适配器实现

不多说直接实现代码:

第一步:首先建议个mian.xml文件文件当中防止一个ExpandableListView控件,

 <ExpandableListView 
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        ></ExpandableListView>
第二步:写一个适配器:
public class ExpandAdapter extends BaseExpandableListAdapter {

    private List<String> group;
    private List<List<String>> child;
    private Context context;
    
    public ExpandAdapter(){}
    public ExpandAdapter(List<String> group,List<List<String>>child,Context context){
        
        this.group = group;
        this.child = child;
        this.context = context;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return this.child.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPositon, int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView childView = getTextView();
        childView.setText(this.child.get(groupPositon).get(childPosition));
        childView.setTextSize(10);
        childView.setTextColor(Color.RED);
        return childView;
    }

    private TextView getTextView(){
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
        TextView textView = new TextView(this.context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36, 0, 0, 0);
        
        return textView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this.child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this.group.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this.group.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpand, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView groupView = getTextView();
        groupView.setText(this.group.get(groupPosition));
        groupView.setTextSize(25);
        groupView.setTextColor(Color.YELLOW);
        return groupView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }

}

第三步:Activity类讲适配器与listView组合


public class MainActivity extends ExpandableListActivity {

    private List<String> group;
    private List<List<String>> child;
    private ExpandableListView listView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_main);
        //
        getExpandableListView().setBackgroundResource(R.drawable.ic_launcher);
        initView();
        getExpandableListView().setAdapter(new ExpandAdapter(this.group,this.child,this));
        //设置拖动列表的时候防止出现黑色背景  
        getExpandableListView().setCacheColorHint(0);
        
    }

    
    private void initView(){
        listView = (ExpandableListView) findViewById(android.R.id.list);
        this.group = new ArrayList<String>();
        this.child = new ArrayList<List<String>>();
        
        addInfo("网球", new String[]{"费德勒","德约科维奇","纳达尔"});
        addInfo("足球运动员", new String[]{"小罗","梅西","马拉多拉"});
        addInfo("篮球运动员", new String[]{"乔丹","科比","勒布朗"});
        
        
    }
    
    private void addInfo(String g,String[] childitem){
        this.group.add(g);
        List<String> list = new ArrayList<String>();
        for (String str : childitem) {
            list.add(str);
        }
        this.child.add(list);
    }
    
    
}

最终运行效果如上所示。
主要用过一个ExpandableListView+自定义一个继承BaseExpandableListAdapter的适配器实现

不多说直接实现代码:

第一步:首先建议个mian.xml文件文件当中防止一个ExpandableListView控件,

 <ExpandableListView 
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        ></ExpandableListView>
第二步:写一个适配器:
public class ExpandAdapter extends BaseExpandableListAdapter {

    private List<String> group;
    private List<List<String>> child;
    private Context context;
    
    public ExpandAdapter(){}
    public ExpandAdapter(List<String> group,List<List<String>>child,Context context){
        
        this.group = group;
        this.child = child;
        this.context = context;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return this.child.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPositon, int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView childView = getTextView();
        childView.setText(this.child.get(groupPositon).get(childPosition));
        childView.setTextSize(10);
        childView.setTextColor(Color.RED);
        return childView;
    }

    private TextView getTextView(){
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
        TextView textView = new TextView(this.context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36, 0, 0, 0);
        
        return textView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this.child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this.group.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this.group.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpand, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView groupView = getTextView();
        groupView.setText(this.group.get(groupPosition));
        groupView.setTextSize(25);
        groupView.setTextColor(Color.YELLOW);
        return groupView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }

}

第三步:Activity类讲适配器与listView组合


public class MainActivity extends ExpandableListActivity {

    private List<String> group;
    private List<List<String>> child;
    private ExpandableListView listView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_main);
        //
        getExpandableListView().setBackgroundResource(R.drawable.ic_launcher);
        initView();
        getExpandableListView().setAdapter(new ExpandAdapter(this.group,this.child,this));
        //设置拖动列表的时候防止出现黑色背景  
        getExpandableListView().setCacheColorHint(0);
        
    }

    
    private void initView(){
        listView = (ExpandableListView) findViewById(android.R.id.list);
        this.group = new ArrayList<String>();
        this.child = new ArrayList<List<String>>();
        
        addInfo("网球", new String[]{"费德勒","德约科维奇","纳达尔"});
        addInfo("足球运动员", new String[]{"小罗","梅西","马拉多拉"});
        addInfo("篮球运动员", new String[]{"乔丹","科比","勒布朗"});
        
        
    }
    
    private void addInfo(String g,String[] childitem){
        this.group.add(g);
        List<String> list = new ArrayList<String>();
        for (String str : childitem) {
            list.add(str);
        }
        this.child.add(list);
    }
    
    
}

最终运行效果如上所示。

 

posted @ 2016-11-28 19:28  天涯海角路  阅读(82)  评论(0)    收藏  举报