2012-08-05 31 views
21

我有一個可擴展的列表視圖。我已經把一些虛擬數據和evrything似乎沒問題,但是當我運行應用程序時,所有組都處於崩潰模式,並且當我點擊其中的每個組時它們都不起作用。這是截圖: enter image description hereAndroid,Expandable列表視圖沒有反應點擊!它不會擴展

集團的XML是:

<?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是:

<?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> 

和main.xml中,擴展列表的定義是:

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

我有一個適配器,我已經寫了這個代碼:

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; 
    } 

} 

終於,在片段活動的代碼,我有:

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; 
      } 
     }); 

} 

當我運行應用程序,並單擊OM父母什麼也沒有發生。我試着在購物類的末尾添加的代碼下面幾行:

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

現在,當我運行應用程序的結果是這樣的截圖: enter image description here

如果我點擊刪除按鈕(父母或子女),我可以看到他們生效(當我檢查logcat),但是當我點擊孩子或父母什麼都沒有發生。所以,我不知道爲什麼回調不起作用。根據我的研究,我發現我需要將圖像按鈕的Focussable設置爲false。正如你在XML文件中看到的那樣,我做到了,但仍然當我點擊父行時,我看不到任何響應。

由於我花了6-7個小時才找到解決方案並且沒有成功,我衷心感謝您的意見和建議。由於

回答

31

最後我找到了解決辦法:)

我不知道是不是bug或者疾病或...

如上所述根據我的研究,我發現,我們需要將焦點設置能力圖像視圖/框爲「假」。我在XML文件中做到了,它不起作用。我將焦點能力設置爲「真」,並在代碼中將其設置爲false。像這樣:

@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; 
    } 

因此根據我的成就!它應該被設置爲「false」,而不是在XML代碼中!現在問題是什麼區別? -我不知道。

+0

它的工作原理。雖然沒有必要在佈局XML文件中定義任何可聚焦屬性。 – emaringolo 2014-05-23 14:40:24

+0

注意:我們需要將android:focusable =「false」放在 2014-08-28 06:17:55

+0

經過數小時的調試,當我通過代碼將焦點設置爲false時,我的可展開列表視圖的onclick開始觸發。這很奇怪。任何人都知道爲什麼? – 2015-09-24 13:20:32

3

需要在佈局xml中爲可擴展列表視圖項目內的可聚焦視圖定義android:focusable="false",但不能在「ExpandableListView」屬性中定義。例如:如果一個自定義父列表項包含一個複選框(它會自動獲得焦點),它應該如下所示。

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