使用自定义适配器实现 Expandable ListView

Posted

技术标签:

【中文标题】使用自定义适配器实现 Expandable ListView【英文标题】:Implement Expandable ListView using Custom Adapter 【发布时间】:2013-07-30 07:27:25 【问题描述】:

我想为 Expandable ListView 实现一个自定义适配器,它由一个 ImageView 和 2 个 TextViews 组成。我已经成功实现了带有阵列适配器的简单可扩展列表视图。以下两个类(第一个是Adapter类,第二个是显示Expandable ListView的Activity)要实现想要的Custom Layout,必须做哪些改动?以下是需要更改的类:

下面是适配器类:

package com.example.travelplanner;

import java.util.List;
import java.util.Map;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter implements OnClickListener

    private Activity context;
    private Map<String, List<String>> itemcollections;
    private List<String> item;
    TextView childtv;
    Button btn_cat_explore;
    public ExpandableListAdapter(Activity context, List<String> item_names, Map<String, List<String>> collections)
        this.context = context;
        this.item = item_names;
        this.itemcollections = collections;
    
    @Override
    public Object getChild(int groupposition, int childposition) 
        // TODO Auto-generated method stub
        return itemcollections.get(item.get(groupposition)).get(childposition);
    

    @Override
    public long getChildId(int groupposition, int childposition) 
        // TODO Auto-generated method stub
        return childposition;
    

    @Override
    public View getChildView(int groupposition, int childpostion, boolean isLastchild, View convertview,
            ViewGroup parent) 
        // TODO Auto-generated method stub
        final String childitem = (String) getChild(groupposition, childpostion);
        LayoutInflater inflater = context.getLayoutInflater();
        if(convertview==null)
            convertview = inflater.inflate(R.layout.child_item, null);
        
        childtv = (TextView)convertview.findViewById(R.id.child_text);
        childtv.setText(childitem);
        return convertview;
    

    @Override
    public int getChildrenCount(int groupposition) 
        // TODO Auto-generated method stub
        return itemcollections.get(item.get(groupposition)).size();
    

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

    @Override
    public int getGroupCount() 
        // TODO Auto-generated method stub
        return item.size();
    

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) 
        // TODO Auto-generated method stub
        String itemname = (String) getGroup(groupPosition);
        if(convertView==null)
            LayoutInflater groupinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = groupinflater.inflate(R.layout.group_item, null);
        
        return convertView;
    

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) 
        // TODO Auto-generated method stub
        return false;
    
    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub

    


下面是我的 TourCatActivity.java:

package com.example.travelplanner;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TourCatActivity extends Activity 

    List<String> groupList;
    List<String> childList;
    Map<String, List<String>> catcollection;
    ExpandableListView expListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tour_cat);
        //Tab view
        TabHost tabHost=(TabHost)findViewById(R.id.tabhost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Category");

        TabSpec spec2=tabHost.newTabSpec("Theme");
        spec2.setIndicator("Theme tours");
        spec2.setContent(R.id.tab2);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);

        createGroupList();

        createCollection();
        expListView = (ExpandableListView) findViewById(R.id.expandableListView1);
        final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
                this, groupList, catcollection);
        expListView.setAdapter(expListAdapter);
        expListView.setOnChildClickListener(new OnChildClickListener() 

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) 
                // TODO Auto-generated method stub
                return false;
            
        );
    
       private void createGroupList() 
            groupList = new ArrayList<String>();
            groupList.add("Rajasthan");
            groupList.add("Golden Triangle");
            groupList.add("Luxury Trains");
            groupList.add("Heritage Tours");
            groupList.add("Cultural Tours");
            groupList.add("Beyond India Tours");
        

       private void createCollection() 
            // preparing category collection(child)
            String[] hpModels =  "HP Pavilion G6-2014TX", "ProBook HP 4540",
                    "HP Envy 4-1025TX" ;
            String[] hclModels =  "HCL S2101", "HCL L2102", "HCL V2002" ;
            String[] lenovoModels =  "IdeaPad Z Series", "Essential G Series",
                    "ThinkPad X Series", "Ideapad Z Series" ;
            String[] sonyModels =  "VAIO E Series", "VAIO Z Series",
                    "VAIO S Series", "VAIO YB Series" ;
            String[] dellModels =  "Inspiron", "Vostro", "XPS" ;
            String[] samsungModels =  "NP Series", "Series 5", "SF Series" ;

            catcollection = new LinkedHashMap<String, List<String>>();

            for (String laptop : groupList) 
                if (laptop.equals("HP")) 
                    loadChild(hpModels);
                 else if (laptop.equals("Dell"))
                    loadChild(dellModels);
                else if (laptop.equals("Sony"))
                    loadChild(sonyModels);
                else if (laptop.equals("HCL"))
                    loadChild(hclModels);
                else if (laptop.equals("Samsung"))
                    loadChild(samsungModels);
                else
                    loadChild(lenovoModels);

                catcollection.put(laptop, childList);
            
        

        private void loadChild(String[] laptopModels) 
            childList = new ArrayList<String>();
            for (String model : laptopModels)
                childList.add(model);
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tour_cat, menu);
        return true;
    


【问题讨论】:

【参考方案1】:

这里是 Expandable ListView 与自定义的最佳示例。如果有任何困难,请告诉我。我想 ExpandableBaseAdapter 是你需要深入研究的东西

【讨论】:

在adapter类的getChildView()方法中,image在哪里实现,因为所有的child都包含相同的image,我想为每个child拥有不同的image,最终,我必须实现imageView 对 group 项目的外观实际上..子项目只包含 textview。 getChildView() 返回的是视图,您可以返回任何视图(也可以扩展布局)以显示在子行内,获取图像数组并设置在位置的基础上 你能编码吗...在 for groupView() 方法中,这样会更清楚。如果可能的话,还要告诉适配器类、MainActivity 类或构造函数等是否必须进行任何更改。 目前我不能自定义,因为我很忙

以上是关于使用自定义适配器实现 Expandable ListView的主要内容,如果未能解决你的问题,请参考以下文章

使用自定义适配器实现过滤器的 ListView 正在获取 IndexOutOfBoundsException

Android中GridView通过自定义适配器实现图文视图排列

动态定义项目时如何创建自定义数组适配器

具有自定义适配器的 ListView 中的 SearchView

Android 屏幕适配屏幕适配通用解决方案 ② ( 自定义组件解决方案 | 需要解决的问题 : 设计稿坐标数据转为屏幕真实坐标数据 | 实现步骤 )

Android 屏幕适配屏幕适配通用解决方案 ② ( 自定义组件解决方案 | 需要解决的问题 : 设计稿坐标数据转为屏幕真实坐标数据 | 实现步骤 )