随笔 - 62  文章 - 0  评论 - 114  阅读 - 18万

android 中onItemClickListener不起作用解决办法

最近写了一个项目,界面使用的是帧布局,里面放置了listview显示联系人,以及右侧有对联系人的字母索引定位。结果在对联系人listview设置onItemClickListener时,发现竟然不起作用。

下面的是布局文件以及设置代码

复制代码
<FrameLayout
android:id="@+id/contact_fl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/contact_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:focusable="true"
android:focusableInTouchMode="true"
android:dividerHeight="1px"
android:scrollbars="none" />
 
<LinearLayout
android:layout_width="28dp"
android:layout_height="match_parent"
android:layout_gravity="right|center"
android:layout_marginBottom="6dp"
android:layout_marginTop="10dp"
android:gravity="right"
android:orientation="vertical"
>
<ListView
android:id="@+id/contact_letter"
android:layout_width="28dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="none"
/>
</LinearLayout>
</FrameLayout>
复制代码

item布局文件

复制代码
View Code
<ImageView
android:scaleType="centerInside"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="3dp"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_contact_picture"
android:id="@+id/contact_contactinfoitem_iv_photo"
/>
 
<TextView 
android:singleLine="true"
android:ellipsize="marquee"
android:textStyle="bold"
android:marqueeRepeatLimit="marquee_forever"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:id="@+id/contact_contactinfoitem_tv_name"
android:text="xxx"
/>
<TextView 
android:singleLine="true"
android:ellipsize="marquee"
android:textStyle="bold"
android:marqueeRepeatLimit="marquee_forever"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:id="@+id/contact_contactinfoitem_tv_phone"
android:text="xxx"
/>
复制代码

在activity中设置onItemCllickListener

复制代码
lv_contact.setOnItemClickListener(new OnItemClickListener()
{
 
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Log.i("my", "onItemClick clicked");
 
}
 
});
复制代码


检查发现onTouchListener里面,返回结果是false,不是true,这意味着屏幕事件是继续传递处理的,不会影响到onItemClickListener的处理。

 去网上查了下发现有说  xml中有个焦点属性会影响onTouchListener,需要将其改为false

再次检测xml文件,里面确实设置有这两个属性

android:focusable="true"
android:focusableInTouchMode="true"

这两个属性的看名字就知道到意思,

android:focusable="true"-------

设置是否获得焦点。若有requestFocus()被调用时,后者优先处理。注意在表单中想设置某一个如EditText获取焦点,光设置这个是不行
的,需要将这个EditText前面的focusable都设置为false才行。在Touch模式下获取焦点需要设置
focusableInTouchMode为true

android:focusableInTouchMode="true"----
 设置在Touch模式下View是否能取得焦点。
 在xml中修改,这属性为false,运行工程,发现还是一样的onItemCllickListener不起作用,这就纠结了。

 因为赶时间,干脆在adapter中getView写item的onclicklistener算了。代码如下:

复制代码
contact_fl.setOnClickListener(new OnClickListener()
{
 
@Override
public void onClick(View view)
{
Log.i("my", "onClick clicked");
 
}
 
});
复制代码

运行项目,item的点击效果有了。但是同时有新的问题出现了。在我的ontouchListener种,onkeydown和onkeyup事件消失了。。。。
只有onkeymove。

经过分析得出结论,那就是肯定有方法或者属性,设置的时候冲突了。
既然item的布局文件中没有button之类的空间。重点查看是否有方法或者属性使得click事件消失了

再次到adapter中仔细检查,发现有个两个这样的方法:

复制代码
@Override
public boolean areAllItemsEnabled()
{
// all items are separator
return false;
}
 
@Override
public boolean isEnabled(int position)
{
// all items are separator
return false;
}
复制代码

查看说明:

Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item). The
result is unspecified if position is invalid. An ArrayIndexOutOfBoundsException should be thrown in that case for fast failure.

 意思就是说,如果当前指定的位置不是一个separator--分隔符(分隔符是一个不能选中,不能点击的item),那么返回true。

那么赶紧改为true,运行项目,效果都有了。

 

 

posted on   nuliniao  阅读(1476)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示