2013-10-17 18 views
14

Tôi đang sử dụng chế độ xem danh sách có thể mở rộng. tôi đưa ra setOnChildClickListener bên trong phương pháp onceate, nhưng setOnChildClickListener không hoạt động, tôi đã tìm kiếm giải pháp trong SO nhưng tôi không thể tìm thấy bất kỳ giải pháp nào. ở đây cho những gì tôi đã làmChế độ xem danh sách có thể mở rộng được đặtOnChildClickListener không hoạt động

public class MenuActivity extends Activity{ 
    ArrayList<MyObject> CatList = new ArrayList<MyObject>(); 
    ArrayList<Object> childItem = new ArrayList<Object>(); 
    ExpandableListView ExList; 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
      setContentView(R.layout.menuactivity); 

     ExList=(ExpandableListView) findViewById(R.id.lvMenu) ; 


      ExList.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 

        ShowItem(CatList.get(childPosition).getId()); 

        Toast.makeText(context, ""+CatList.get(childPosition).getId(), 1).show(); 
       // TODO Auto-generated method stub 
       return false; 
      } 
     }); 

    } 


public void ShowItem(int id) 
{ 
    // do something 
} 

    public class ElistAdapt extends BaseExpandableListAdapter { 

public ArrayList<MyObject> groupItem, tempGrpChild; 
public ArrayList<Object> Childtem = new ArrayList<Object>(); 
public LayoutInflater minflater; 
public Activity activity; 


public ElistAdapt(ArrayList<MyObject> GroupList, ArrayList<Object> childItem) { 
    groupItem = GroupList; 
    this.Childtem = childItem; 
} 

public void setInflater(LayoutInflater mInflater, Activity act) { 
    this.minflater = mInflater; 
    activity = act; 
} 

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

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return 0; 
} 

@Override 
public View getChildView(final int groupPosition, final int childPosition, 
    boolean isLastChild, View convertView, ViewGroup parent) { 
    tempGrpChild = (ArrayList<MyObject>) Childtem.get(groupPosition); 
    TextView text = null; 
    TextView text2 = null; 
    if (convertView == null) { 
    convertView = minflater.inflate(R.layout.childrow, null); 
    } 
    text = (TextView) convertView.findViewById(R.id.textView1); 
    text2 = (TextView) convertView.findViewById(R.id.textView2); 
    text.setText(tempGrpChild.get(childPosition).getName()); 
    text2.setText(tempGrpChild.get(childPosition).getPrice()); 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return ((ArrayList<MyObject>) Childtem.get(groupPosition)).size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
// return null; 
    return this.groupItem.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return groupItem.size(); 
} 

@Override 
public void onGroupCollapsed(int groupPosition) { 
    super.onGroupCollapsed(groupPosition); 
} 

@Override 
public void onGroupExpanded(int groupPosition) { 
    super.onGroupExpanded(groupPosition); 
} 

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

@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.grouprow, null); 
      } 

      TextView tvGroupName = (TextView) convertView.findViewById(R.id.textView1); 
      tvGroupName.setText(groupItem.get(groupPosition).getName()); 


    return convertView; 
} 

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

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

} 
} 

hãy giúp tôi tại sao setOnChildClickListener không hoạt động

+1

Trong boolean công khaiChildClick ... trả về true. – TheFlash

+1

Bạn đã đặt bố cục hoạt động của mình ở đâu? 'setContentView (R.layout.main);'? – GrIsHu

+0

@Jesbin MJ Bạn đã đăng mã hoàn chỉnh chưa? – GrIsHu

Trả lời

8

cuối cùng tôi đã làm việc của mình, tôi loại bỏ các nhấp chuột được, đặt tiêu điểm từ xml R.layout.childrow. hiện đang hoạt động tốt

+0

Làm việc cho tôi quá :) – biswajitGhosh

28

Trong lớp bộ điều hợp của bạn trả về giá trị thực trong phương thức isChildSelectable().

+0

Vì vậy, đơn giản, rất ngu ngốc;) – Warpzit

+0

hey tôi có một mối quan tâm. Đối với tôi một nửa của nó làm việc ... Trông lạ, phải không? Ý tôi là, nó đang làm việc lần đầu tiên, nếu tôi bấm lại một đứa trẻ, nó không hoạt động. Bạn có biết tại sao? –

0

@Jesbin MJ bạn đã cứu mạng tôi !!!

Vui lòng cho bất kỳ ai gặp sự cố tương tự: xóa thuộc tính có thể nhấp = "true" khỏi bố cục! Nó cuối cùng đã làm việc cho tôi :)

Các vấn đề liên quan