可扩展的ListView中的Android onChildClickListener

时间:2014-10-10 11:09:17

标签: android expandablelistview expandablelistadapter

我有一个expandableListView,其中有三个组项,每组包含五个子项。我想在所有组的每个子项上给出onClick事件,并且在每个不同的页面上使用Intent显示。

我所做的代码结果如下。 onChildClickListener适用于第一组子项。但是当我点击其他两个组的子项时,它再次显示我为第一组子项目提供的页面。我在下面发布我的代码。请有人建议我解决一些问题。

MoreActivity.java

package info.demo.goa;

public class MoreActivity extends Activity {

private ExpandListAdapter ExpAdapter;
private ArrayList<Group> ExpListItems;
private ExpandableListView ExpandList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.more_tab);

    ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
    ExpListItems = SetStandardGroups();
    ExpAdapter = new ExpandListAdapter(MoreActivity.this, ExpListItems);
    ExpandList.setAdapter(ExpAdapter);

    ExpandList.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id)
        {
            if(childPosition==0)
            {
                Intent i=new Intent(getApplicationContext(), Basilica_ch.class);
                startActivity(i);
            }
            if(childPosition==1)
            {
                Intent i=new Intent(getApplicationContext(), StFrancis_ch.class);
                startActivity(i);
            }
            if(childPosition==2)
            {
                Intent i=new Intent(getApplicationContext(), Lady_ch.class);
                startActivity(i);
            }
            if(childPosition==3)
            {
                Intent i=new Intent(getApplicationContext(), StThomas_ch.class);
                startActivity(i);
            }
            if(childPosition==4)
            {
                Intent i=new Intent(getApplicationContext(), SeCathedral_ch.class);
                startActivity(i);
            }
            if(childPosition==5)
            {
                Intent i=new Intent(getApplicationContext(), Aguada_f.class);
                startActivity(i);
            }
            if(childPosition==6)
            {
                Intent i=new Intent(getApplicationContext(), Chapora_f.class);
                startActivity(i);
            }
            if(childPosition==7)
            {
                Intent i=new Intent(getApplicationContext(), CaboDerama_f.class);
                startActivity(i);
            }
            if(childPosition==8)
            {
                Intent i=new Intent(getApplicationContext(), Rachol_f.class);
                startActivity(i);
            }
            if(childPosition==9)
            {
                Intent i=new Intent(getApplicationContext(), Terekhol_f.class);
                startActivity(i);
            }
            if(childPosition==10)
            {
                Intent i=new Intent(getApplicationContext(), Archa_m.class);
                startActivity(i);
            }
            if(childPosition==11)
            {
                Intent i=new Intent(getApplicationContext(), Calizz_m.class);
                startActivity(i);
            }
            if(childPosition==12)
            {
                Intent i=new Intent(getApplicationContext(), GoaState_m.class);
                startActivity(i);
            }
            if(childPosition==13)
            {
                Intent i=new Intent(getApplicationContext(), Naval_m.class);
                startActivity(i);
            }
            if(childPosition==14)
            {
                Intent i=new Intent(getApplicationContext(), Palacio_m.class);
                startActivity(i);
            }
            return false;
        }
    });

}

public ArrayList<Group> SetStandardGroups() {

    String group_names[] = { "Churches", "Forts", "Museums"  };


    String country_names[] = { "Basilica of Bom Jesus", "Church and Convent of St Francis of    Assisi",
            "Church of Our Lady of Immaculate Conception", "Church of St. Thomas", "Se Cathedral", 
            "The Aguada Fort", "The Chapora Fort", "The Cabo de Rama Fort", "Rachol Fort", "Terekhol Fort",
            "Archaeological Museum", "Calizz Museum", "Goa State Museum", "Naval Aviation Museum ", "Palacio Do deao"
            };

    int Images[] = { 
            R.drawable.basilicaofbomjesus, R.drawable.stfrancisofassisi,     R.drawable.churchofourladyofimmaculateconception,
            R.drawable.stthomas, R.drawable.secathedral, R.drawable.fortaguda,
            R.drawable.chaporafort, R.drawable.cabodarama, R.drawable.racholfort,
            R.drawable.terekholfort, R.drawable.archaeologicalmuseum, R.drawable.calizzmuseum,
            R.drawable.goastatemuseum, R.drawable.navalaviationmuseum, R.drawable.palaciododeao,
           };

    ArrayList<Group> list = new ArrayList<Group>();

    ArrayList<Child> ch_list;

    int size = 5;
    int j = 0;

    for (String group_name : group_names) {
        Group gru = new Group();
        gru.setName(group_name);

        ch_list = new ArrayList<Child>();
        for (; j < size; j++) {
            Child ch = new Child();
            ch.setName(country_names[j]);
            ch.setImage(Images[j]);
            ch_list.add(ch);
        }
        gru.setItems(ch_list);
        list.add(gru);

        size = size + 5;
    }

    return list;
}

class  ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        Child child = (Child) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_item, null);
        }
        ImageView iv = (ImageView) convertView.findViewById(R.id.imgItemImage);
        TextView tv = (TextView) convertView.findViewById(R.id.txtItemName);



        tv.setText(child.getName().toString());
        iv.setImageResource(child.getImage());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.size();
    }

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

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

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        return convertView;
    }

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

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

}

}


 Child.java

package info.demo.goa;

public class Child {

private String Name;
private int Image;

public String getName() {
    return Name;
}

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

public int getImage() {
    return Image;
}

public void setImage(int Image) {
    this.Image = Image;
}
}

 Group.java

package info.demo.goa;

import java.util.ArrayList;

public class Group {

private String Name;
private ArrayList<Child> Items;

public String getName() {
    return Name;
}

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

public ArrayList<Child> getItems() {
    return Items;
}

public void setItems(ArrayList<Child> Items) {
    this.Items = Items;
}

 }

XML代码

 more_tab.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ExpandableListView 
    android:id="@+id/exp_list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">   
</ExpandableListView>

 </RelativeLayout>


 child_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">

<ImageView
    android:id="@+id/imgItemImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_marginLeft="5dp"
    android:layout_weight="0.92" >

    <TextView
        android:id="@+id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="@android:color/black"/>
</LinearLayout>

<ImageView
    android:id="@+id/imgItemArrow"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:src="@drawable/arrow_icon"
    android:layout_marginRight="5dp"/>

  </LinearLayout>


   group_item.xml


<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/group_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="25dp"
    android:textStyle="bold"/>
 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

If Group Position is 1 then 
1 - 0 , 1, 2, 3, 4
2 - 0 , 1, 2, 3, 4 but not 5, 6, 7, 8, 9
3 - 0 , 1, 2, 3, 4 but not 10, 11, 12, 13, 14
@Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        if (groupPosition == 0) {
            if (childPosition == 0) {
                Intent i = new Intent(getApplicationContext(), Basilica_ch.class);
                startActivity(i);
            }
            if (childPosition == 1) {
                Intent i = new Intent(getApplicationContext(), StFrancis_ch.class);
                startActivity(i);
            }
            if (childPosition == 2) {
                Intent i = new Intent(getApplicationContext(), Lady_ch.class);
                startActivity(i);
            }
            if (childPosition == 3) {
                Intent i = new Intent(getApplicationContext(), StThomas_ch.class);
                startActivity(i);
            }
            if (childPosition == 4) {
                Intent i = new Intent(getApplicationContext(), SeCathedral_ch.class);
                startActivity(i);
            }
        }
        if (groupPosition == 1) {
            if (childPosition == 0) {
                Intent i = new Intent(getApplicationContext(), Aguada_f.class);
                startActivity(i);
            }
            if (childPosition == 1) {
                Intent i = new Intent(getApplicationContext(), Chapora_f.class);
                startActivity(i);
            }
            if (childPosition == 2) {
                Intent i = new Intent(getApplicationContext(), CaboDerama_f.class);
                startActivity(i);
            }
            if (childPosition == 3) {
                Intent i = new Intent(getApplicationContext(), Rachol_f.class);
                startActivity(i);
            }
            if (childPosition == 4) {
                Intent i = new Intent(getApplicationContext(), Terekhol_f.class);
                startActivity(i);
            }
        }
        if (groupPosition == 2) {
            if (childPosition == 0) {
                Intent i = new Intent(getApplicationContext(), Archa_m.class);
                startActivity(i);
            }
            if (childPosition == 1) {
                Intent i = new Intent(getApplicationContext(), Calizz_m.class);
                startActivity(i);
            }
            if (childPosition == 2) {
                Intent i = new Intent(getApplicationContext(), GoaState_m.class);
                startActivity(i);
            }
            if (childPosition == 3) {
                Intent i = new Intent(getApplicationContext(), Naval_m.class);
                startActivity(i);
            }
            if (childPosition == 4) {
                Intent i = new Intent(getApplicationContext(), Palacio_m.class);
                startActivity(i);
            }
        }
        return false;
    }