1

我有一个 BaseExpandableListAdapter 看起来像这样:

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final ContentViewHolder contentViewHolder;
    View view = convertView;
    final Book book = getChild(groupPosition, childPosition); 
    if (view == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        view = inflater.inflate(R.layout.item_book_row, null);
        contentViewHolder = new ContentViewHolder();
        contentViewHolder.tvName = (TextView) view.findViewById(R.id.tvName);
        contentViewHolder.chkSelected = (CheckBox) view.findViewById(R.id.chkSelected);
        view.setTag(contentViewHolder);
    }else{
        contentViewHolder = (ContentViewHolder) view.getTag();
    }        

    contentViewHolder.tvName.setText(book.getTitle());
    contentViewHolder.chkSelected.setChecked(checkStates[groupPosition][childPosition]);

    contentViewHolder.chkSelected.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            book.setSelected(contentViewHolder.chkSelected.isChecked());
            checkStates[groupPosition][childPosition] = contentViewHolder.chkSelected.isChecked();
            Ln.d("check on header %d row %d", groupPosition, childPosition);

        }
    });

    return view;
}

  public boolean isChildSelectable(int groupPosition, int childPosition) {
     Ln.d("Child selected "+groupPosition+" : "+childPosition);    
    return true;
}

与子布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<!-- Row data -->
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TextView>
    <CheckBox
        android:id="@+id/chkSelected"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="@dimen/item_vertical_margin"
        android:layout_marginRight="@dimen/item_vertical_margin" />  
  </RelativeLayout>

这是片段视图:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >    
<ExpandableListView
    android:id="@+id/lstCatBook"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" 
    android:groupIndicator="@null">
  </ExpandableListView>
 </LinearLayout>

然后我setOnChildClickListener在我的片段上注册。

  lstCatBook.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            Log.d("Click on group " +groupPosition +" child " + childPosition);
            return false;
        }
    });

当我单击行项目或复选框时,一切都很好。但问题是当我将列表视图滚动到底部时,我无法再次单击行项目(onChildClick未触发),而复选框可以工作
我对这个问题没有任何想法。我已经搜索并尝试了很多方法,例如: set check box focusable = falsedescendantFocusability=blocksDescendants但没有任何帮助。
我哪里错了?任何帮助,将不胜感激。

4

1 回答 1

2

最后我发现了问题:因为如果列表视图,我对所有组使用 set Expand。我不知道为什么setOnChildClickListener在这种情况下不起作用。但是我可以通过从 getChildView 捕获转换视图上的点击事件然后通知主视图来解决。

像这样的东西:

public View getChildView(final int groupPosition, final int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {

  //load data
    convertView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
       // notify for main view by using another listener
    }
   });
   return converview;
 }
于 2013-09-06T17:10:44.933 回答