发生环境:
1.GridView的xml布局
<GridView
android:id="@+id/nsdk_layout_rg_ugc_event_page_grid_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/d2uc_dimen_32"
android:layout_marginBottom="@dimen/lion_ugc_content_center_item_margin_bottom"
android:layout_marginEnd="@dimen/d2uc_dimen_32"
android:listSelector="@android:color/transparent"
android:descendantFocusability="afterDescendants"
android:horizontalSpacing="@dimen/d2uc_dimen_8">
2.item的布局
<?xml version="1.0" encoding="utf-8"?>
<TextView android:fontFamily="@font/gms_ui_thin" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:minWidth="@dimen/navi_rg_ugc_report_listview_text_width_new"
android:layout_height="@dimen/navi_rg_ugc_event_hint_item_height"
android:textSize="@dimen/lion_common_text_size_13_dp"
android:background="@drawable/selector_button_focus"
android:focusable="true"
android:textColor="@color/text_primary_nor"
android:layout_marginRight="@dimen/navi_rg_ugc_report_panel_gridview_spacing_margin_right_new"
android:gravity="center">
</TextView>
java代码:
detailGridView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ugcCommonTextAdapter.setSelectedPos(position);
ugcCommonTextAdapter.notifyDataSetChanged();
}
});
原因:
1.GridView布局中添加了属性android:descendantFocusability="afterDescendants"
2.自布局中添加了 android:focusable="true" 属性,导致子view可以获取焦点
由于afterDescendants属性,所以子view获取焦点的同时便拦截了ItemClick事件
其他说明:
descendantFocusability属性:
descendantFocusability是View的一个属性。可以理解是viewGroup和其子控件焦点相关的属性。
通过这个属性可以指定viewGroup和其子View到底谁获取焦点, 直接在viewGroup上使用就行。
下面是这个属性的三种属性值:
beforeDescendants :viewGroup会优先其子类控件而获取到焦点
afterDescendants :viewGroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants :viewGroup会覆盖子类控件而直接获得焦点
解决方法有三种:
第一种:自定义view的点击事件利用接口实现ItemClick事件监听
adapter中代码添加接口
private UgcCommonTextAdapterCallBack callBack;
public interface UgcCommonTextAdapterCallBack {
void onClick(View view ,int position);
}
public void setCallBack(UgcCommonTextAdapterCallBack callBack) {
this.callBack = callBack;
}
并在adapter的getView方法中添加view的onclick事件利用接口传递出去,实现自定义ItemClick事件
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view = super.getView(position, convertView, parent);view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (callBack != null) { callBack.onClick(view, position); } } }); return view; }
最后adapter的引用现实该接口,自定义的itemClick事件就完成了
ugcCommonTextAdapter.setCallBack(new UgcCommonTextAdapter.UgcCommonTextAdapterCallBack() {
@Override
public void onClick(View view, int position) {
ugcCommonTextAdapter.setSelectedPos(position);
ugcCommonTextAdapter.notifyDataSetChanged();
}
});
第二种:去掉 android:descendantFocusability="afterDescendants" 属性
第三种:去掉子VIew中的android:focusable="true" 属性,
文章讲述了在GridView布局中设置descendantFocusability属性导致ItemClick事件拦截的问题,提供了解决方案:自定义事件接口、移除descendantFocusability属性以及移除子View的focusable属性。
825

被折叠的 条评论
为什么被折叠?



