2012-12-31 92 views
2

我花了2個小時試圖實現OnChildClickListener on ExpandableListView並不起作用。我找不到,我犯了一個錯誤....ExpandableListView OnChildClickListener不工作

這裏是我的適配器類: ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context mContext; 
    private ExpandableListView mExpandableListView; 
    private List<Grupe> mGroupCollection; 
    private int[] groupStatus; 


    public ExpandableListAdapter(){} 
    public ExpandableListAdapter(Context pContext, 
      ExpandableListView pExpandableListView, 
      List<Grupe> pGroupCollection) { 
     mContext = pContext; 
     mGroupCollection = pGroupCollection; 
     mExpandableListView = pExpandableListView; 
     groupStatus = new int[mGroupCollection.size()]; 

     setListEvent(); 
    } 

    private void setListEvent() { 

     mExpandableListView 
       .setOnGroupExpandListener(new OnGroupExpandListener() { 

        @Override 
        public void onGroupExpand(int arg0) { 
         // TODO Auto-generated method stub 
         groupStatus[arg0] = 1; 
        } 
       }); 

     mExpandableListView 
       .setOnGroupCollapseListener(new OnGroupCollapseListener() { 

        @Override 
        public void onGroupCollapse(int arg0) { 
         // TODO Auto-generated method stub 
         groupStatus[arg0] = 0; 
        } 
       }); 
    } 

    @Override 
    public String getChild(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     //return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name; 
     return mGroupCollection.get(arg0).getChildren().get(arg0).getPrimaoc_Poruke(); 
    } 

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

    @Override 
    public View getChildView(int arg0, int arg1, boolean arg2, View arg3, 
      ViewGroup arg4) { 
     // TODO Auto-generated method stub 

     ChildHolder childHolder; 
     if (arg3 == null) { 
      arg3 = LayoutInflater.from(mContext).inflate(
        R.layout.list_group_item, null); 

      childHolder = new ChildHolder(); 

      childHolder.title = (TextView) arg3.findViewById(R.id.item_title); 
      arg3.setTag(childHolder); 
     }else { 
      childHolder = (ChildHolder) arg3.getTag(); 
     } 

     //childHolder.title.setText(mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name); 
     childHolder.title.setText(mGroupCollection.get(arg0).getChildren().get(arg1).getPrimaoc_Poruke()); 
     return arg3; 
    } 

    @Override 
    public int getChildrenCount(int arg0) { 
     // TODO Auto-generated method stub 
     //return mGroupCollection.get(arg0).GroupItemCollection.size(); 
     return mGroupCollection.get(arg0).getChildren().size(); 
    } 

    @Override 
    public Object getGroup(int arg0) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0); 
    } 

    @Override 
    public int getGroupCount() { 
     // TODO Auto-generated method stub 
     return mGroupCollection.size(); 
    } 

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

    @Override 
    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { 
     // TODO Auto-generated method stub 
     GroupHolder groupHolder; 
     if (arg2 == null) { 
      arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group, 
        null); 
      groupHolder = new GroupHolder(); 
      groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img); 
      groupHolder.title = (TextView) arg2.findViewById(R.id.group_title); 
      arg2.setTag(groupHolder); 
     } else { 
      groupHolder = (GroupHolder) arg2.getTag(); 
     } 
     if (groupStatus[arg0] == 0) { 
      groupHolder.img.setImageResource(R.drawable.group_down); 
     } else { 
      groupHolder.img.setImageResource(R.drawable.group_up); 
     } 
    // groupHolder.title.setText(mGroupCollection.get(arg0).Name); 
     groupHolder.title.setText(mGroupCollection.get(arg0).getTip()); 

     return arg2; 
    } 

    class GroupHolder { 
     ImageView img; 
     TextView title; 
    } 

    class ChildHolder { 
     TextView title; 
    } 

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

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

} 

MainActivity類別

public class Glavna extends Activity implements OnClickListener, OnChildClickListener{ 

private List<Grupe> mGroupCollection; //grupe korisnika 
    private ExpandableListView mExpandableListView; 
    private ExpandableListAdapter adapter=null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_glavna); 
     System.out.println("POCINJEM SAAAAAAAA RADOM"); 

prepareResource(); 
     initPage(); 
} 

private void initPage() { 
     mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1); 

     adapter = new ExpandableListAdapter(this, 
       mExpandableListView, mGroupCollection); 

     mExpandableListView.setAdapter(adapter); 


     mExpandableListView.setOnChildClickListener(this); 

    } 
private void prepareResource() { 


     mGroupCollection = new ArrayList<Grupe>(); 
      ge.setTip("Online korisnici"); 
       mGroupCollection.add(ge); 

    AktivniChat gi = new AktivniChat(1, "primaoc 1", new Korisnik(1, "posiljaoc1")); 
    mGroupCollection.get(0).setAktivniChat(gi); 



    } 

@Override 
    public boolean onChildClick(ExpandableListView parent, View v, 
      int groupPosition, int childPosition, long id) { 
     // TODO Auto-generated method stub 

     System.out.println("test"); 
     return true; 
    } 

} 

回答

5

解決方案在這裏:

這是我的ExpandableListView:

enter image description here

xml文件(設置機器人:可調焦=上的所有元素 「假」

<?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:baselineAligned="false" 
    android:paddingTop="5dip" 
    android:focusable="false"> 

    <LinearLayout 
     android:id="@+id/groupItem" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#FFF385" 
     android:clickable="false" 
     android:focusable="false"> 

     <TextView 
      android:id="@+id/item_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#FF84" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="sample" 
      android:textColor="#000000" 
      android:textSize="20sp" /> 

    </LinearLayout> 

</LinearLayout> 

這OnChildClickListener完美的工作後:)

+0

爲我工作,任何人都知道這是爲什麼?這是一個錯誤? – Joucks

+0

據我所知,這不是一個錯誤。 childView中的視圖只需捕獲onClick()事件,因此事件永遠不會到達childView。 (這就是爲什麼onChildClickListener永遠不會被調用) – Endzeit

0

它的工作對我來說太。 ..佈局中的所有項目(子項)必須設置爲focusable =「false」,clickable =「false」。但是如果你在子佈局中有按鈕,這是行不通的。

0

不要忘記一個孩子能夠可選擇屬性..

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return true; 
} 
相關問題