2013-02-14 36 views
5

Tôi đang sử dụng expandablelistview. Có anyway tôi có thể mở rộng hàng nhóm khi một hình ảnh bên trong được nhấp? Tôi biết rằng để có hình ảnh phản ứng với nhấp chuột, tôi phải đặt trọng tâm của nó. Bây giờ, khi hình ảnh này phản hồi nhấp chuột (bên trong bộ điều hợp theo yêu cầu của tôi), làm cách nào tôi có thể mở rộng/thu gọn theo lập trình một hàng nhóm cụ thể mà nó thuộc về?Mở rộng hàng nhóm theo lập trình trong danh sách mở rộng

Cảm ơn bạn

Trả lời

11

Trong bộ chuyển đổi

private OnItemSelectedListener onItemSelectedCallback; 

public interface OnItemsSelectedListener { 
    public void onImageSelected(int groupPos); 
} 

public YourAdapter(Context context) { 
    try { 
     this.onItemSelectedCallback = (OnItemSelectedListener) context; 
    } 
    catch (ClassCastException e) { 
     throw new ClassCastException(context.toString() + " must implement OnItemSelectedListener "); 
    } 
} 

Thêm này trong getView()

ImageView imageView = new ImageView(); 
imageView.setTag(R.id.tagGroupPosition, groupPosition); 
imageView.setOnClickListener(onClickListener); 

Thêm này trong OnClickListener

OnClickListener onClickListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     int groupPos = ((ImageView) v).getTagId(R.id.tagGroupPosition); 
     onItemSelectedCallback.onImageSelected(groupPos); 
    } 
} 

Sau đó, trong hoạt động bạn phải cụ Yo urAdapter.OnItemsSelectedListener Ghi đè onImageSelected

@Override 
public void onImageSelected(int groupPos){ 
    if(expandableList.isGroupExpanded(groupPos)){ 
     expandableList.collapseGroup(groupPos); 
    }else{ 
     expandableList.expandGroup(groupPos); 
    } 
} 
+0

Đâu là sự sụp đổ/mở rộng câu trả lời đó là lý do đằng sau câu hỏi của tôi? – Snake

+0

Tôi có thêm mã để làm rõ câu trả lời của mình, vui lòng xem nó. Hy vọng điều này có thể giúp bạn. – Pongpat

+0

Cảm ơn bạn đã trả lời. Đã chấp nhận – Snake

2

Một cách đơn giản hơn:

  1. Vượt qua một tham chiếu đến ExpandableListView trong constructor bộ chuyển đổi;
  2. trong getGroupView thêm một OnClickListener vào nút bạn cần và chỉ cần gọi listView.expandGroup(position);

Ví dụ:

public class YourAdapter extends BaseExpandableListAdapter { 

private ExpandableListView listView; 

public ReportsAdapter(ExpandableListView listView) { 
    this.listView = listView; 
} 

//... 

@Override 
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) { 
    ParentViewHolder holder; 

    if (convertView == null) { 
     convertView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.your_item, parent, false); 

     holder = new ParentViewHolder(); 

     //... holder logic 


     holder.btnExpand = convertView.findViewById(R.id.btn_expand); 

     convertView.setTag(holder); 
    } else { 
     holder = (ParentViewHolder) convertView.getTag(); 
    } 

    //..holder logic 

    holder.btnExpand.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      listView.expandGroup(position); 
     } 
    }); 

    return convertView; 
} 

Trong hoạt động của bạn onCreate() hoặc Fragment onCreateView()

ExpandableListView listView = (ExpandableListView) view.findViewById(R.id.expandable_list_view); 
    mAdapter = new YourAdapter(listView); 
    listView.setAdapter(mAdapter); 
Các vấn đề liên quan