2012-08-05 28 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"/>