ExpandableListView可点击的孩子

时间:2013-11-16 08:45:40

标签: android parent-child expandablelistview

我有一个ExpandableListView,我需要通过点击每个孩子来烘烤不同的消息,我不知道如何实现它

这是我的CategoriesActivity.java

 public class CategoriesActivity extends ExpandableListActivity {
    private List<Group> mListGroup;
    private List<ItemDetail> mListTeam1;
    private List<ItemDetail> mListTeam2;
    private List<ItemDetail> mListTeam3;
    private List<ItemDetail> mListTeam4;
    private List<ItemDetail> mListTeam5;
    private List<ItemDetail> mListTeam6;
    private List<ItemDetail> mListTeam7;
    private List<ItemDetail> mListTeam8;

    private CustomAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories_layout);

        getExpandableListView().setOnItemClickListener(
                new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> a, View v,
                            int position, long id) {


                    }
                });


        setDummyData();

        mAdapter = new CustomAdapter(this, mListGroup);

        getExpandableListView().setAdapter(mAdapter);

    }

    private void setDummyData() {
        mListGroup = new ArrayList<Group>();
        mListTeam1 = new ArrayList<ItemDetail>();
        mListTeam2 = new ArrayList<ItemDetail>();
        mListTeam3 = new ArrayList<ItemDetail>();
        mListTeam4 = new ArrayList<ItemDetail>();
        mListTeam5 = new ArrayList<ItemDetail>();
        mListTeam6 = new ArrayList<ItemDetail>();
        mListTeam7 = new ArrayList<ItemDetail>();
        mListTeam8 = new ArrayList<ItemDetail>();

        mListGroup.add(new Group("رایانه", mListTeam1));
        mListGroup.add(new Group("صوتی و تصویری", mListTeam2));
        mListGroup.add(new Group("خانگی و اداری", mListTeam3));
        mListGroup.add(new Group("شبکه و ارتباطات", mListTeam4));
        mListGroup.add(new Group("نرم افزار و رسانه", mListTeam5));
        mListGroup.add(new Group("بازی", mListTeam6));
        mListGroup.add(new Group("سلامت", mListTeam7));
        mListGroup.add(new Group("تلفن و موبایل", mListTeam8));

        mListTeam1.add(new ItemDetail("لپ تاپ", 1));
        mListTeam1.add(new ItemDetail("سخت افزار رایانه", 2));
        mListTeam1.add(new ItemDetail("حافظه", 3));
        mListTeam1.add(new ItemDetail("ماوس", 4));
        mListTeam1.add(new ItemDetail("تبلت و لوازم جانبی", 5));
        mListTeam1.add(new ItemDetail("صفحه کلید", 6));

        mListTeam2.add(new ItemDetail("دوربین عکاسی و فیلم برداری", 7));
        mListTeam2.add(new ItemDetail("چند رسانه ای", 8));
        mListTeam2.add(new ItemDetail("صوتی", 9));
        mListTeam2.add(new ItemDetail("تصویری", 10));
        mListTeam2.add(new ItemDetail("لوازم جانبی", 11));

        mListTeam3.add(new ItemDetail("تجهیزات فروشگاهی", 12));
        mListTeam3.add(new ItemDetail("اسکنر و پرینتر", 13));
        mListTeam3.add(new ItemDetail("پاک کننده", 14));
        mListTeam3.add(new ItemDetail("ماشین حساب", 15));
        mListTeam3.add(new ItemDetail("لوازم مصرفی", 16));
        mListTeam3.add(new ItemDetail("لوازم جانبی الکتریکی", 17));

        mListTeam4.add(new ItemDetail("تجهیزات شبکه", 18));
        mListTeam4.add(new ItemDetail("کابل و لوازم جانبی شبکه", 19));

        mListTeam5.add(new ItemDetail("نرم افزار", 20));

        mListTeam6.add(new ItemDetail("اسباب بازی الکترونیکی", 21));
        mListTeam6.add(new ItemDetail("سخت افزار", 22));

        mListTeam7.add(new ItemDetail("سلامت فردی", 23));
        mListTeam7.add(new ItemDetail("بهداشت عمومی", 24));

        mListTeam8.add(new ItemDetail("فکس و تلفن", 25));
        mListTeam8.add(new ItemDetail("موبایل", 26));
        mListTeam8.add(new ItemDetail("لوازم جانبی", 27));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.categories_layout, menu);
        return true;
    }

 }

我的customAdapter

public class CustomAdapter extends BaseExpandableListAdapter
{

public LayoutInflater   mInflater;
private List<Group>     mGroup;


public CustomAdapter(Context context, List<Group> group)
{
    mInflater = LayoutInflater.from(context);
    mGroup = group;
}

@Override
public Object getChild(int groupPos, int childPos)
{
    return mGroup.get(groupPos).getItemList().get(childPos);
}

@Override
public long getChildId(int groupPosition, int childPosition)
{
    return mGroup.get(groupPosition).getItemList().get(childPosition).get_id();
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent)
{
    View v = convertView;
    if (v == null)
        v = mInflater.inflate(R.layout.child, null);

    TextView childName = (TextView) v.findViewById(R.id.childName);

    ItemDetail itemDetail = mGroup.get(groupPosition).getItemList().get(childPosition);

    childName.setText(itemDetail.getName());

    return v;
}

@Override
public int getChildrenCount(int groupPosition)
{
    return mGroup.get(groupPosition).getItemList().size();
}

@Override
public Object getGroup(int groupPosition)
{
    return mGroup.get(groupPosition);
}

@Override
public int getGroupCount()
{
    return mGroup.size();
}

@Override
public long getGroupId(int groupPosition)
{
    return mGroup.get(groupPosition).get_id();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    View v = convertView;
    if (v == null)
        v = mInflater.inflate(R.layout.root, null);

    TextView group = (TextView) v.findViewById(R.id.root);

    Group gr = mGroup.get(groupPosition);

    group.setText(gr.getName());
    return v;
}

@Override
public boolean hasStableIds()
{
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
    return false;
}

}

我的Group.java

import java.util.List;

public class Group
{
private int                 _id;
private String              name;
private List<ItemDetail>    itemList;

public Group(String name, List<ItemDetail> itemList)
{
    this.name = name;
    this.itemList = itemList;
}

public int get_id()
{
    return _id;
}

public void set_id(int _id)
{
    this._id = _id;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public List<ItemDetail> getItemList()
{
    return itemList;
}

public void setItemList(List<ItemDetail> itemList)
{
    this.itemList = itemList;
}

}

和我的ItemDetails.java

public class ItemDetail
{

private int     idItem;
private String  name;
private String  desc;

public ItemDetail(String name ,int _idItem)
{
    this.name = name;
    this.idItem= _idItem;
}

public int get_id()
{
    return idItem;
}

public void set_id(int _idItem)
{
    this.idItem = _idItem;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public String getDesc()
{
    return desc;
}

public void setDesc(String desc)
{
    this.desc = desc;
}

}

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

expListView.setOnChildClickListener(new OnChildClickListener(){

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            final String selected = (String) expListAdapter.getChild(
                    groupPosition, childPosition);
            Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG)
                    .show();

            return true;
        }