作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的 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 = false
、descendantFocusability=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/
我是一名优秀的程序员,十分优秀!