2012-08-05 14 views
21

Mam rozwijany widok listy. Położyłem trochę obojętnych danych i wszystko wydaje się w porządku, ale kiedy uruchamiam aplikację, wszystkie grupy są w trybie zwinięcia i nie działają po kliknięciu każdego z nich. jest to zrzut ekranu: enter image description hereAndroid, widok listy rozwijanej nie reaguje na kliknięcie !!! nie rozwija się

XML Grupy jest:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/bg_slider" 
    android:orientation="horizontal" > 


    <TextView 
     android:id="@+id/tvRecipeName" 
     style="@style/normal_text.bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="50dp" 
     android:layout_marginRight="5dp" 
     android:focusable="false" 
     android:lines="1" 
     android:maxLines="1" 
     android:text="@string/dd_title" /> 


    <ImageButton 
     android:id="@+id/ibDeleteRecipe" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="15dp" 
     android:background="@color/transparent" 
     android:contentDescription="@string/cd" 
     android:focusable="false" 
     android:src="@android:drawable/ic_menu_delete" /> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/tvRecipeName" 
     android:focusable="false" /> 

</RelativeLayout> 

XML dziecka:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/White" 
    android:gravity="left|center_vertical" > 

    <View 
     android:layout_width="1dp" 
     android:layout_height="35dp" 
     android:layout_toLeftOf="@+id/tvIngredient" 
     android:focusable="false" /> 

    <TextView 
     android:id="@+id/tvIngredient" 
     style="@style/normal_text_black" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="5dp" 
     android:lines="1" 
     android:maxLines="1" 
     android:text="@string/dd_title" 
     android:focusable="false" /> 

    <ImageButton 
     android:id="@+id/ibDeleteIngredient" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="15dp" 
     android:background="@color/transparent" 
     android:contentDescription="@string/cd" 
     android:focusable="false" 
     android:src="@android:drawable/btn_dialog" /> 

</RelativeLayout> 

aw main.xml, definicja rozszerzalnej liście jest:

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

Mam adapter, który napisałem dla tego kodu:

public class ExpAdapter extends BaseExpandableListAdapter { 

    private final String TAG = "ExpAdapter"; 
    private Context context; 
    static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"}; 
    static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" }, 
               {"Ponting", "Adam Gilchrist", "Michael Clarke"}, 
               {"Andrew Strauss", "kevin Peterson", "Nasser Hussain"}, 
               {"Graeme Smith", "AB de villiers", "Jacques Kallis"} }; 

    public ExpAdapter(Context context) { 
     this.context = context; 

     Log.i(TAG, "Adapter created."); 
    } 


    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return null; 
    } 

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

     return 0; 
    } 

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

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.elv_child, null); 
     } 

     TextView tvItem = (TextView) convertView.findViewById(R.id.tvIngredient); 
     ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ibDeleteIngredient); 
     ibDelete.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i(TAG, "******"); 
      } 
     }); 
     tvItem.setText(arrChildelements[groupPosition][childPosition]); 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return arrChildelements[groupPosition].length; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return null; 
    } 

    @Override 
    public int getGroupCount() { 
     return arrGroupelements.length; 
    } 

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.elv_group, null); 
     } 

     TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName); 
     ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe); 
     ibDeleteRcipe.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i(TAG, "%%%%%%"); 
      } 
     }); 
     tvItem.setText(arrGroupelements[groupPosition]); 

     return convertView; 
    } 

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

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

} 

i wreszcie w kodzie aktywności fragmentu mam:

public class ShoppingList extends FragmentActivity {  
     ExpAdapter adapter = new ExpAdapter(this); 
     //Linking expnadable list view 
     expListView = (ExpandableListView) findViewById(R.id.elv); 
     expListView.setAdapter(adapter); 
     expListView.setOnGroupExpandListener(new OnGroupExpandListener() { 
      @Override 
      public void onGroupExpand(int groupPosition) { 
       Log.i(TAG, "Group " + groupPosition + " expanded."); 
      } 
     }); 
     expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { 
      @Override 
      public void onGroupCollapse(int groupPosition) { 
       Log.i(TAG, "Group " + groupPosition + " collapsed."); 
      } 
     }); 
     expListView.setOnChildClickListener(new OnChildClickListener() { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
       Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked."); 
       return false; 
      } 
     }); 

} 

Kiedy uruchomić aplikację i kliknij om rodzicom nic się nie dzieje. Próbowałem dodać następujące wiersze kodu na końcu klasy zakupy:

int count = adapter.getGroupCount(); 
for (int position = 1; position <= count; position++) 
    expListView.expandGroup(position - 1); 

Teraz gdy uruchamiam wynikiem aplikacja jest jak to zdjęcie: enter image description here

Jeśli kliknięciu na przycisk Delete (rodzica lub dziecka), Widzę, że działają (jak sprawdzam logcat), jednak gdy klikam na dziecko lub rodzic nic się nie dzieje. Tak więc, nie mam pojęcia, dlaczego callbacks nie działa. Na podstawie moich badań stwierdziłem, że muszę ustawić Focussable przycisków obrazu na false. Jak widać w plikach XML zrobiłem to, ale nadal, gdy klikam na wiersze nadrzędne i podrzędne, nie widzę żadnej odpowiedzi.

Ponieważ spędziłem ponad 6-7 godzin, aby znaleźć rozwiązanie i nie powiodło się, bardzo dziękuję za uwagi i sugestie. Dzięki

Odpowiedz

31

Wreszcie znalazłem rozwiązanie :)

nie mam pojęcia czy to bug czy choroba lub ...

Jak wspomniano powyżej, na podstawie moich badań stwierdziliśmy, że musimy ustawić ostrość-zdolność z widoku obrazu/box na "false". Zrobiłem to w pliku XML i nie zadziałało. Ustawiłem fokus-zdolność na "true" w XML i ustawiłem w kodzie na false. w ten sposób:

@Override 
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.elv_group, null); 
     } 

     TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName); 
     ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe); 
     ibDeleteRcipe.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) {    
       ... 
      } 
     }); 
     ibDeleteRcipe.setFocusable(false); 

     tvItem.setText(arrGroupElements[groupPosition]); 

     return convertView; 
    } 

W związku z tym moje osiągnięcie! powinien być ustawiony na "false" w kodzie nie w XML! Teraz pytanie brzmi: Czym jest różnica? -Nie mam pojęcia.

+0

Działa.Chociaż nie jest konieczne definiowanie jakiegokolwiek atrybutu z możliwością ustawiania ostrości w pliku XML układu. – emaringolo

+0

Uwaga: musimy umieścić android: focusable = "false" w

+0

Po wielu godzinach debugowania, gdy ustawiłem możliwość ustawienia ostrości na wartość false za pomocą kodu, uruchomiono funkcję onclick mojego rozwijanego listview. To bardzo dziwne. Ktoś wie dlaczego? –

3

Konieczne jest zdefiniowanie android:focusable="false" w układzie xml dla widoków możliwych do ustawienia w widoku rozwijanym, ale nie w atrybucie "ExpandableListView".

Np .: jeśli niestandardowy element listy nadrzędnej zawiera pole wyboru (automatycznie uzyska fokus), powinien wyglądać jak poniżej.

<CheckBox 
    android:id="@+id/cbkSelectDownload" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false"/> 
Powiązane problemy