2012-08-19 15 views
5

Ich bin Anfänger für Android-Programmierung. Ich habe den Code von http://shenhengbin.wordpress.com heruntergeladen und funktioniert gut, aber das einzige Problem ist, löst nicht die onChildClick. HierAndroid benutzerdefinierte Expandablelistview löst nicht onChildClick

ist die Haupttätigkeit

public class MenuCategory extends Activity { 
     private ExpandableListView mExpandableListView; 
     private List<GroupEntity> mGroupCollection; 
     ExpandableListAdapter adapter; 

     @Override 
     public void onCreate(Bundle savedInstanceStater) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.menucategory); 

      prepareResource(); 
      initPage(); 

     } 


     private void prepareResource() { 
      String data = null; 
      FileInputStream fIn = null; 
      InputStreamReader isr = null; 
      String response = null; 
      String getParam = "?get=menuCategory"; 
      try{ 
       fIn = openFileInput("fromfile.dat"); 
       isr = new InputStreamReader(fIn); 
       int size = fIn.available(); 
       char[] inputBuffer = new char[size]; 
       isr.read(inputBuffer); 
       data = new String(inputBuffer); 
       isr.close(); 
       fIn.close(); 
       String[] dataSep = data.split("#"); 
       String Hs = dataSep[0]; 

       try { 
        response = CustomHttpClient.executeHttpGet(Hs+getParam); 
        String res = response.toString(); 
        if(response.length()!=0){ 
         mGroupCollection = new ArrayList<GroupEntity>(); 
         JSONObject jsonObject = null; 
         JSONArray eventArray = new JSONArray(res); 
         for (int i = 0; i < eventArray.length(); i++) { 
          jsonObject = eventArray.getJSONObject(i); 
          GroupEntity ge = new GroupEntity(); 
          ge.Name = jsonObject.getString("Cat"); 

          JSONArray abc = jsonObject.getJSONArray("SubCat"); 
          for (int j=0; j<abc.length(); j++){ 
           GroupItemEntity gi = ge.new GroupItemEntity(); 
           gi.Name = abc.getString(j); 
           ge.GroupItemCollection.add(gi); 
          } 
          mGroupCollection.add(ge); 
         } 
        } 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      }catch(IOException e){ 
       e.printStackTrace(System.err); 
      } 
     } 

     private void initPage() { 
      mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
      adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection); 
      mExpandableListView.setAdapter(adapter); 
      registerForContextMenu(mExpandableListView); 
     } 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
      String string = "Child Click"; 
      int duration = Toast.LENGTH_SHORT; 
      Toast toast = Toast.makeText(getApplicationContext(), string, duration); 
      toast.show(); 
      return false; 
     } 
    } 

Hier ist die Gewohnheit BaseExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context mContext; 
    private ExpandableListView mExpandableListView; 
    private List<GroupEntity> mGroupCollection; 
    private int[] groupStatus; 
    private int lastExpandedGroupPosition; 

    public ExpandableListAdapter(Context pContext, 
      ExpandableListView pExpandableListView, 
      List<GroupEntity> pGroupCollection) { 
     mContext = pContext; 
     mGroupCollection = pGroupCollection; 
     mExpandableListView = pExpandableListView; 
     groupStatus = new int[mGroupCollection.size()]; 

     setListEvent(); 
    } 

    private void setListEvent() { 

     mExpandableListView 
       .setOnGroupExpandListener(new OnGroupExpandListener() { 

        public void onGroupExpand(int arg0) { 
         // TODO Auto-generated method stub 
         groupStatus[arg0] = 1; 
         lastExpandedGroupPosition = arg0; 
        } 
       }); 

     mExpandableListView 
       .setOnGroupCollapseListener(new OnGroupCollapseListener() { 

        public void onGroupCollapse(int arg0) { 
         // TODO Auto-generated method stub 
         groupStatus[arg0] = 0; 
        } 
       }); 
    } 

    public String getChild(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name; 
    } 

    public long getChildId(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public View getChildView(int arg0, int arg1, boolean arg2, View arg3, 
      ViewGroup arg4) { 
     // TODO Auto-generated method stub 

     ChildHolder childHolder; 
     if (arg3 == null) { 
      arg3 = LayoutInflater.from(mContext).inflate(
        R.layout.list_group_item, null); 

      childHolder = new ChildHolder(); 

      childHolder.title = (TextView) arg3.findViewById(R.id.item_title); 
      arg3.setTag(childHolder); 
     }else { 
      childHolder = (ChildHolder) arg3.getTag(); 
     } 

     childHolder.title.setText(mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name); 
     return arg3; 
    } 

    public int getChildrenCount(int arg0) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0).GroupItemCollection.size(); 
    } 

    public Object getGroup(int arg0) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0); 
    } 

    public int getGroupCount() { 
     // TODO Auto-generated method stub 
     return mGroupCollection.size(); 
    } 

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

    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { 
     // TODO Auto-generated method stub 
     GroupHolder groupHolder; 
     if (arg2 == null) { 
      arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group, 
        null); 
      groupHolder = new GroupHolder(); 
      groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img); 
      groupHolder.title = (TextView) arg2.findViewById(R.id.group_title); 
      arg2.setTag(groupHolder); 
     } else { 
      groupHolder = (GroupHolder) arg2.getTag(); 
     } 
     if (groupStatus[arg0] == 0) { 
      groupHolder.img.setImageResource(R.drawable.group_down); 
     } else { 
      groupHolder.img.setImageResource(R.drawable.group_up); 
     } 
     groupHolder.title.setText(mGroupCollection.get(arg0).Name); 

     return arg2; 
    } 

    class GroupHolder { 
     ImageView img; 
     TextView title; 
    } 

    class ChildHolder { 
     TextView title; 
    } 

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

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

    @Override 
    public void onGroupExpanded(int groupPosition){ 
     //collapse the old expanded group, if not the same 
     //as new group to expand 
     if(groupPosition != lastExpandedGroupPosition){ 
      mExpandableListView.collapseGroup(lastExpandedGroupPosition); 
     } 

     super.onGroupExpanded(groupPosition);   
     lastExpandedGroupPosition = groupPosition; 
    } 
} 

Artikelgruppe Layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/group_header_bg" 
    android:gravity="center_vertical" > 

    <ImageView 
     android:id="@+id/tag_img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="10dip" 
     android:src="@drawable/group_down" 
     android:focusable="false" 
     /> 

    <TextView 
     android:id="@+id/group_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="10dip" 
     android:text="" 
     android:textColor="#57810f" 
     android:textSize="18dip" 
     android:textStyle="bold" 
     android:focusable="false" 
     /> 

</RelativeLayout> 

und hier ist der Artikel Kind Layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:paddingTop="5dip" > 

    <LinearLayout 
     android:id="@+id/groupItem" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/play_group_item" 
     android:clickable="true" > 


     <TextView 
      android:id="@+id/item_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="15dip" 
      android:layout_marginLeft="15dip" 
      android:text="sample" 
      android:textColor="#FFF38585" 
      android:textSize="20sp" 
      android:focusable="false" 
      /> 

    </LinearLayout> 

</LinearLayout> 

bitte helfen Sie mir

+0

Ich sehe nirgendwo in Ihrem Code, wo Sie den Listener für untergeordnete Klicks festlegen, um tatsächlich auf diese Ereignisse zu warten. – Luksprog

+0

wenn ich mExpandableListView.setOnChildClickListener hinzufügen (neu onChildClickListener() { \t \t \t public boolean onChildClick (ExpandableListView Elternteil, Blick v, int groupPosition, int Child, long id) { \t \t String string = "Child Klicken Sie auf"; \t \t \t int duration = Toast.LENGTH_SHORT; \t \t \t Toast Toast = Toast.makeText (getApplicationContext(), string, Dauer); \t \t Toast.Show(); \t \t Rückgabe false; \t \t} \t \t}); onCreate() zeigt mir den Fehler in der Eclipse, da onChildClickListener nicht in einen Typ – Manu

+0

@Manu aufgelöst werden kann. Ich habe auch das gleiche Tutorial verwendet. Bitte sagen Sie mir, wie die Aktion beim Klicken unter Kind ausgeführt wird. – mukesh

Antwort

0

den Hörer einzurichten für Sie Kind klickt es so in der initPage Methode tun können (nicht onCreate()):

mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
      adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection); 
      mExpandableListView.setAdapter(adapter); 
registerForContextMenu(mExpandableListView); 
mExpandableListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 
       String string = "Child Click"; 
       int duration = Toast.LENGTH_SHORT; 
       Toast toast = Toast.makeText(getApplicationContext(), string, 
         duration); 
       toast.show(); 
       return false; 
      } 
     }); 
+0

Sorry, geben Sie mir den gleichen Fehler – Manu

+0

@Manu Und was sagt Ihnen der Fehler? Sind Sie sicher, dass Sie die richtigen Importe haben: 'import android.widget.ExpandableListView;' und 'import android.widget.ExpandableListView.OnChildClickListener;'? – Luksprog

+0

Ja, ich habe importiert. Aber gibt mir Fehler wie: Die Methode setOnChildClickListener (ExpandableListView.OnChildClickListener) in der Art ExpandableListView für die \t Argumente nicht anwendbar ist (neue OnChildClickListener() {}) \t - OnChildClickListener kann nicht auf einen Typ – Manu

13

In Adapter haben Sie eine Methode namens isChildSelectable () Hexe kehrt falsch zurück, das ist das Problem. Ändern Sie den zurückgegebenen Wert in "true", um das Problem zu beheben.

+1

Dies sollte als die richtige Antwort vor langer Zeit gemacht worden sein .. es ist diese geerbte Methode unbemerkt bleiben, das hat mich alle meine Haare zupfen lassen: P – LeoFarage

+0

Es wäre hilfreich, wenn Sie eine richtige Antwort gewählt haben, so könnte es auch anderen helfen oder die Lösung teilen, die schließlich für Sie funktioniert hat. –

1

Ich weiß, es ist alter Thread, aber wenn jemand hier ist, um die Antwort zu finden, ist dies für sie. Ich fand das gleiche Problem, wenn ich this tutorial for expandable list view verwendet, um anklickbare untergeordnete Element einfach zu "Hauptgruppe Element Layout" gehen (in meinem Code ist es list_group_item) und machen linearlayout und textview klickbare und facusable false.

4

Ich war mit diesem Problem konfrontiert, onChildClick wurde nicht ausgelöst.

ich diesen Kommentar gefunden:

gelöst, das Problem ist, dass, wenn Sie mehrere Textview ist auf dem childview haben, die fokussierbar Eigenschaft falsch sein muss (ich weiß nicht genau, warum) Zugabe android: descendantFocusability = "blocksDescendants" zu der Ansicht löste das Problem

auf Android ExpandableListView and onChildClick not working

So Zugabe android:descendantFocusability="blocksDescendants" auf childView für mich gearbeitet.

+2

Dies war die einzige Lösung, die für mich funktionierte. Andere häufige Vorschläge wie das Festlegen von isChildSelectable(), um "True" zurückzugeben, waren erforderlich, aber nicht ausreichend, um das Problem zu lösen. –

+0

Genie !!! Das war die Lösung für mich – Merlevede

Verwandte Themen