2012-05-21 61 views
1

我有一個可擴展的列表視圖,當我點擊一個排它激發了onChildClickonGroupClick取決於如果我點擊一個孩子或一個組。OnChildClickListener消失

enter image description here

如果我添加在XML佈局文件可點擊的東西(如一個複選框)的onChildClick沒有了火。 (如果我添加了一個簡單的TextView它的工作原理藏漢)

the child_layout

任何人都知道這是爲什麼?該項目是否覆蓋了偵聽器?

的主要代碼:適配器的

 View firstView = inflater.inflate(R.layout.main_layout, container, false); 
listView = (ExpandableListView) firstView.findViewById(R.id.expandableListView1); 
listView.setOnChildClickListener(new OnChildClickListener() 
     { 

      @Override 
      public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4) 
      { 
       Toast.makeText(getActivity().getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show(); 
       return false; 
      } 
     }); 

代碼:

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context context; 

    private ArrayList<String> groups; 

    private ArrayList<ArrayList<FarmaciListItem>> children; 

    public ExpandableListAdapter(Context context, ArrayList<String> groups, 
      ArrayList<ArrayList<FarmaciListItem>> children) { 
     this.context = context; 
     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; 
    } 

    // Return a child view. You can load your custom layout here. 
    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
      View convertView, ViewGroup parent) { 
     FarmaciListItem farmacoItem = (FarmaciListItem) getChild(groupPosition, childPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_layout, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.tvChild); 
     tv.setText(" " + farmacoItem.getName()); 

     // Depending upon the child type, set the imageTextView01 
     tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 
     if (farmacoItem instanceof PesoItem) { 
      tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0); 
     } 
     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; 
    } 

    // Return a group view. You can load your custom layout here. 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
      ViewGroup parent) { 
     String group = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.group_layout, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.tvGroup); 
     tv.setText(group); 
     return convertView; 
    } 
+0

請顯示代碼以及! – SpeedBirdNine

回答

2

的問題是,因爲你有你的孩子可聚焦元素。例如,考慮一個按鈕,默認情況下它將處於專注狀態。所以將它設置爲false將做到這一點。

在你的情況,也許是因爲複選框添加下面的屬性

android:focusable="false" 

這應該做的伎倆。

+0

確切地說,現在它完美地工作,謝謝所有的答覆;如果我點擊它調用onChildClick的行,並且如果我單擊該行內的複選框,它會調用現在在適配器中設置的偵聽器。 – Mark

+0

是的。我已經通過這個..沒有問題。任何幫助。任何時候。 :) –

2

集合android:focusable="false"爲可對焦元素例如複選框