gpt4 book ai didi

android - 带有复选框行的 ExpandableListView OnChildClickListener 在滚动时不会触发

转载 作者:行者123 更新时间:2023-11-29 21:36:32 28 4
gpt4 key购买 nike

我有一个如下所示的 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>

这是 fragment View :

  <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>

然后我在我的 fragment 上注册 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;
}
});

当我点击行项目或复选框时一切正常。但问题是当我将 ListView 滚动到底部时,我无法再次单击行项目(onChildClick 未触发),而复选框可以工作
我对这个问题没有想法。我搜索并尝试了很多方法,例如:设置复选框 focusable = falsedescendantFocusability=blocksDescendants 但无济于事。
我哪里错了?任何帮助,将不胜感激。

最佳答案

最后我弄清楚了问题所在:因为我使用 set Expand for all group if listview。我不知道为什么 setOnChildClickListener 在这种情况下不起作用。但我可以通过从 getChildView 捕获转换 View 上的点击事件然后通知主视图来解决。

类似的东西:

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;
}

关于android - 带有复选框行的 ExpandableListView OnChildClickListener 在滚动时不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18306476/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com