9

Nie mogę do końca życia zrozumieć, dlaczego mój ExpandableListView nie rozwija się ... Użyłem instrukcji log w prawie każdym kliknięciu, które mogę znaleźć dla ExpandableListView i nie wygląda na to, że ktoś z nich zostanie wezwany.android expandablelistview nie rozwija się ani nie odbiera zdarzeń kliknięcia

Wiem, że jest wiele postów na ten temat, ale przeczytałem je wszystkie i próbowałem wielu rzeczy i nie mam szczęścia, mam nadzieję, że brakuje mi drobnego błędu, który będzie łatwy do wykrycia dla kogoś innego.

Główna działalność:

public class ForumListActivity extends Activity { 

    private static ArrayList<Forum> forumList; 
    private static ArrayList<ArrayList<SubForum>> subForumList; 
    private ExpandableListView forumListView; 
    private ForumListAdapter forumListAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.main_page); 
     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

     forumListView = (ExpandableListView) this.findViewById(R.id.main_page_forum_list); 

     forumList = new ArrayList<Forum>(); 
     subForumList = new ArrayList<ArrayList<SubForum>>(); 
     setUpForums(this); 

     forumListAdapter = new ForumListAdapter(this, forumList, subForumList); 
     forumListView.setAdapter(forumListAdapter); 

     forumListView.setOnGroupExpandListener(new OnGroupExpandListener(){ 
      @Override 
      public void onGroupExpand(int groupPosition) { 
       Log.d("onGroupExpand", "this works?"); 
       for(int i=0; i<forumListAdapter.getGroupCount(); i++) { 
        if(i != groupPosition) 
         forumListView.collapseGroup(groupPosition); 
       } 
      } 
     }); 

     forumListView.setOnGroupClickListener(new OnGroupClickListener() { 
      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
       Log.d("onGroupClick:", "worked"); 
       parent.expandGroup(groupPosition); 
       return true; 
      } 
     }); 
    } 

Uwaga: setUpForums() metoda po prostu ma tablic systemowych i umieszcza je w forumList i subForumList

ListViewAdapter:

public class ForumListAdapter extends BaseExpandableListAdapter { 

    private ArrayList<Forum> groups; 
    private ArrayList<ArrayList<SubForum>> children; 
    private Context ctx; 

    public ForumListAdapter(Context ctx, ArrayList<Forum> groups, ArrayList<ArrayList<SubForum>> children) { 
     this.ctx = ctx; 
     this.groups = groups; 
     this.children = children; 
    } 



    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return children.get(groupPosition).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) { 
     if (convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(ctx); 
      convertView = inflater.inflate(R.layout.forum_list_child_item_row, null); 
     } 

     SubForum currentSubForum = children.get(groupPosition).get(childPosition); 
     TextView name = (TextView)convertView.findViewById(R.id.child_row_forum_title); 
     TextView desc = (TextView)convertView.findViewById(R.id.child_row_forum_description); 

     if (name != null) 
      name.setText(currentSubForum.getName()); 

     if (desc != null) 
      desc.setText(currentSubForum.getDescription()); 

     convertView.setFocusableInTouchMode(true); 
     return convertView; 
    } 



    @Override 
    public int getChildrenCount(int groupPosition) { 
     return children.get(groupPosition).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) { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = LayoutInflater.from(ctx); 
      convertView = inflater.inflate(R.layout.forum_list_group_item_row, null); 
     } 

     Forum currentForum = (Forum) groups.get(groupPosition); 
     TextView name = (TextView) convertView.findViewById(R.id.group_item_forum_title); 
     //ImageView image = (ImageView) convertView.findViewById(R.id.group_item_expander_image); 

     if(name != null) 
      name.setText(currentForum.getName());   

     /* 
     if(image != null) { 
      int[][] group_state_sets = {{}, {android.R.attr.state_expanded}}; 
      image.setVisibility(View.VISIBLE); 
      int stateSetIndex = (isExpanded ? 1 : 0) ; 
      Drawable drawable = image.getDrawable(); 
      drawable.setState(group_state_sets[stateSetIndex]); 
     } 
     */ 

     return convertView; 
    } 



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



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


} 

Układ Grupa:

<?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:background="@drawable/turquoise_gradient" 
    android:orientation="horizontal" 
    android:paddingTop="6dip" 
    android:paddingBottom="6dip" 
    android:paddingLeft="6dip" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/turquoise_gradient" 
     android:orientation="vertical" 
     android:padding="2dip" > 

     <TextView 
      android:id="@+id/group_item_forum_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|left" 
      android:gravity="left" 
      android:paddingLeft="5dip" 
      android:textColor="@color/white" 
      android:textSize="16dip" /> 

    </LinearLayout> 

    <!-- 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:gravity="center|right"> 

     <ImageView 
      android:id="@+id/group_item_expander_image" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:gravity="center" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/collapse_down" /> 


    </LinearLayout> --> 

</LinearLayout> 
Układ

dziecko:

<?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:background="@drawable/turquoise_gradient" 
    android:orientation="horizontal" 
    android:paddingTop="6dip" 
    android:paddingBottom="6dip" 
    android:paddingLeft="6dip" > 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="2dip" 
     android:background="@drawable/turquoise_gradient" > 

     <TextView 
      android:id="@+id/child_row_forum_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:layout_gravity="center_vertical" 
      android:paddingLeft="5dip" 
      android:textColor="@color/white" 
      android:maxLines="1" 
      android:textSize="11dip" /> 

     <TextView 
      android:id="@+id/child_row_forum_description" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:layout_gravity="center_vertical" 
      android:paddingLeft="15dip" 
      android:textColor="@color/white" 
      android:maxLines="2" 
      android:textSize="11dip" /> 

    </LinearLayout> 

</LinearLayout> 

główny układ strony:

<?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:background="@color/black" 
    android:orientation="vertical" > 

    <ExpandableListView 
     android:id="@+id/main_page_forum_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/black" 
     android:divider="@color/black" 
     android:dividerHeight="1dip" 
     android:clickable="true" /> 

</LinearLayout> 

Każda pomoc można zapewnić mile widziana!

+0

jej może być bo nie masz dane dziecko nie mają u debugowany – Rakshi

+0

spróbować dodając pewną domyślną wartość tekstu w twoim układzie Childs dla textviews – Rakshi

Odpowiedz

21

mam również napotkał podobny problem jak ty. Po kilku dniach śledztwa odkryłem, że zrobiłem coś złego. Naprawiłem go, aby działał poprawnie, dokonując drobnych zmian.

Spójrzmy na ciało boolean onGroupClick(...) w setOnGroupClickListener.Pan wrócił prawdziwą to znaczy "the click was handled"

Należy return false jeśli chcesz się rozwijać. Więc proponuję zrobić tak:

forumListView.setOnGroupClickListener(new OnGroupClickListener() { 
    @Override 
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
      Log.d("onGroupClick:", "worked"); 
      parent.expandGroup(groupPosition); 
      return false; 
     } 
    }); 

w android.widget.ExpandableListView klasy, jest metoda o nazwie boolean handleItemClick(View v, int position, long id) który jest odpowiedzialny za rozszerzenie/zwijanie grupy lub przechodząc na kliknięcia do prawidłowego dziecka.

/* It's a group click, so pass on event */ 
     if (mOnGroupClickListener != null) { 
      if (mOnGroupClickListener.onGroupClick(this, v, 
        posMetadata.position.groupPos, id)) { 
       posMetadata.recycle(); 
       return true; 
      } 
     } 

    /* expanding/collapsing/other tasks... */ 

jeśli wdrożenie onGroupClick do powrót prawda, The 8th kod poniżej linii nigdy nie będzie stracony. (to oznacza, że ​​grupy nigdy nie zostaną zwinięte, rozwinięte)

Mam nadzieję, że moja odpowiedź pomogła ci :-) Powodzenia!

+0

Właśnie uratowałeś 3 dni bólów głowy. Thanks i +1 za wspaniałą odpowiedź –

+0

dziękuję za to! –

6

Są trzy rzeczy, prawdopodobnie u trzeba sprawdzić,

  1. sprawdzić, czy u mieć żadnych danych dla chid, bo jeśli u dont mieć jakiekolwiek dane dziecko nie pojawi się w ogóle.

2.try eli sprawdzić stan podczas wykorzystujące inflaters układ

if (convertView == null) { 
    LayoutInflater inflater = LayoutInflater.from(ctx); 
    convertView = inflater.inflate(R.layout.forum_list_child_item_row, null); 
    } 
  1. trzeba też zdać Viewgroup tutaj

    convertView = inflater.inflate(R.layout.forum_list_child_item_row,parent, false); 
    
+0

Złe dane były moim problemem, Jeszcze raz dziękuję !! –

0

Dodaj implements OnGroupExpandListener do swojej aktywności. Wtedy to zadziała. Używam tego samego działa dobrze.

0

Podczas pracy z rozwijanymi listami rozszerzeniem grupy jest domyślna funkcjonalność. Oznacza to, że grupa będzie rozwijać się dopiero po kliknięciu na niego dawca trzeba overrirde onGroupExpand (int groupPosition) lub innej metody po prostu wypełnić swoje dane do listy somthing jak poniżej:

public class MyActivity extends Activity { 

    private ExpandableListView forumListView; 
    private ForumListAdapter forumListAdapter; 
    String[] forumList={"group 1","group 2","group 3"}; 
String[][] subForumList={{"group 1 child1","group 1 child1","group 1 child3"}, 
        {"group 2 child1","group 2 child2","group 2 child3"}, 
        {"group 3 child1","group 3 child2","group 3 child3"}, 
        }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
this.setContentView(R.layout.main); 


forumListView = (ExpandableListView) this.findViewById(R.id.main_page_forum_list); 




forumListAdapter = new ForumListAdapter(this, forumList, subForumList); 
forumListView.setAdapter(forumListAdapter); 



    /* forumListView.setOnGroupExpandListener(new OnGroupExpandListener(){ 
    public void onGroupExpand(int groupPosition) { 
     Log.d("onGroupExpand", "this shit works?"); 
     for(int i=0; i<forumListAdapter.getGroupCount(); i++) { 
      if(i != groupPosition) 
       forumListView.collapseGroup(groupPosition); 
     } 
    } 
}); 

forumListView.setOnGroupClickListener(new OnGroupClickListener() { 
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
     Log.d("onGroupClick:", "worked"); 
     parent.expandGroup(groupPosition); 
     return true; 
    } 
});*/ 
    } 

    public class ForumListAdapter extends BaseExpandableListAdapter { 

     String[] groups; 
    String[][] children; 
    private Context ctx; 

    public ForumListAdapter(Context ctx, String[] groups, String[][] children) { 
    this.ctx = ctx; 
    this.groups = groups; 
    this.children = children; 
} 

public Object getChild(int arg0, int arg1) { 
    // TODO Auto-generated method stub 
    return children[arg0][arg1]; 
} 

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

public View getChildView(int arg0, int arg1, boolean arg2, View arg3, 
     ViewGroup arg4) { 
    if (arg3 == null) { 
     LayoutInflater inflater = LayoutInflater.from(ctx); 
     arg3 = inflater.inflate(R.layout.child, null); 
    } 

    String childData = children[arg0][arg1]; 
    TextView name = (TextView)arg3.findViewById(R.id.child_row_forum_title); 
    TextView desc = (TextView)arg3.findViewById(R.id.child_row_forum_description); 

    if (name != null) 
     name.setText(childData); 

    if (desc != null) 
     // desc.setText(currentSubForum.getDescription()); 

    arg3.setFocusableInTouchMode(true); 
    return arg3;} 

public int getChildrenCount(int arg0) { 
    // TODO Auto-generated method stub 
    return children[arg0].length; 
} 

public Object getGroup(int arg0) { 
    // TODO Auto-generated method stub 
    return groups[arg0]; 
} 

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

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

public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { 
    if (arg2 == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(ctx); 
     arg2 = inflater.inflate(R.layout.group, null); 
    } 


    TextView name = (TextView) arg2.findViewById(R.id.group_item_forum_title); 
    //ImageView image = (ImageView) arg2.findViewById(R.id.group_item_expander_image); 

    if(name != null) 
     name.setText(groups[arg0]);   

    /* 
    if(image != null) { 
     int[][] group_state_sets = {{}, {android.R.attr.state_expanded}}; 
     image.setVisibility(View.VISIBLE); 
     int stateSetIndex = (isExpanded ? 1 : 0) ; 
     Drawable drawable = image.getDrawable(); 
     drawable.setState(group_state_sets[stateSetIndex]); 
    } 
    */ 

    return arg2;} 

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

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

     } 
3

Upewnij się, że niestandardowy układ grupy nie ma android:textIsSelectable="false" jako "prawda", jeśli tekst w widoku tekstowym jest ustawiony do wybrania, rozszerzalny widok listy rozszerzyłby się w pierniku, ale nie w jellybean i może nie działać również w ICS.

1

Miałem podobny problem i został rozwiązany przez usunięcie właściwości android:clickable="true" z ExpandableListView na xml.

0

forumListView.collapseGroup (groupPosition);

należy

forumListView.collapseGroup (i);

10

Jeśli masz widżet na elemencie listy, taki jak przycisk, być może musisz dodać do niego android:focusable="false". Przycisk nie pozwalał na kliknięcie elementu listy. Tak było w moim przypadku.

+0

Dziękuję bardzo! –

+0

To mi pomogło. – Omolara

+0

Może to nie działać, jeśli robisz to w xml. Dodaj button.setFocusable (false); w języku Java. – Immy

1

wiem, że to był już odpowiedział, ale spróbuj ustawić układ bazowy cokolwiek jesteś pompowania mieć atrybut:

android:descendantFocusability="blocksDescendants" 
Powiązane problemy