ListView的item点击事件

android_Y 2014-03-20 05:45:30
有一个ListView,每项里面有个button。
现在的问题是我需要点击项有Selector效果,button也有Selector效果。
写Selector时点击item时button的Selector也被触发了。
我希望点击item时button的Selector不被触发。
望大神解决
...全文
2180 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
android_Y 2014-03-21
  • 打赏
  • 举报
回复
引用 11 楼 u012356374 的回复:
是的,把对应属性颜色变回去
可以使用了Shape改就不方便,还得单独写成xml.
android_Y 2014-03-21
  • 打赏
  • 举报
回复
引用 10 楼 hjywyj 的回复:
方法1: 别用TextView,改为Button,并加上focusable <Button android:id="@+id/btn" android:focusable="false" 方法2: 在Adapter的getView里给btn绑定ontouch
view.findViewById(R.id.btn).setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				if (MotionEvent.ACTION_DOWN == action)
					v.setSelected(true);
				else if (MotionEvent.ACTION_MOVE != action)
					v.setSelected(false);
				return false;
			}
		});
修改selector_shape_btn_blue.xml <item android:state_pressed="true"> 为 <item android:state_selected="true"> 如果还有更简单的方法请回复我,谢谢!
我之前写的就是Button 我是为了试试TextView能不能用所以才改的, <item android:state_pressed="true"> 为 <item android:state_selected="true">修改后Button没有效果。 touch之前我就用过,不过这个不太方便。 所以我想看看能不能改好。直接使用Selector。
tanrunj 2014-03-21
  • 打赏
  • 举报
回复
是的,把对应属性颜色变回去
  • 打赏
  • 举报
回复
方法1: 别用TextView,改为Button,并加上focusable <Button android:id="@+id/btn" android:focusable="false" 方法2: 在Adapter的getView里给btn绑定ontouch
view.findViewById(R.id.btn).setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				if (MotionEvent.ACTION_DOWN == action)
					v.setSelected(true);
				else if (MotionEvent.ACTION_MOVE != action)
					v.setSelected(false);
				return false;
			}
		});
修改selector_shape_btn_blue.xml <item android:state_pressed="true"> 为 <item android:state_selected="true"> 如果还有更简单的方法请回复我,谢谢!
android_Y 2014-03-21
  • 打赏
  • 举报
回复
引用 4 楼 u012356374 的回复:
可以尝试加入这三个属性
android:clickable="false"
       	 	android:focusable="false"
       		android:focusableInTouchMode="false"
实在不行可以在OnItemClickListener里面进行强制修改
加属性没效果 在OnItemClickListener里强制修改是怎么修改?把button的颜色变回去?
android_Y 2014-03-21
  • 打赏
  • 举报
回复
selector_shape_btn_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="3dp" />
            <solid android:color="@color/red" />
        </shape>
	</item>
    <item>
        <shape>
            <corners android:radius="3dp" />
            <solid android:color="@color/blue" />
        </shape>
	</item>
</selector>
selector_shape_btn_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="2dip"/>
            <solid android:color="@color/gray_btn_fouce" />
        </shape>
	</item>
    <item>
		<shape>
		    <corners android:radius="2dip"/>
            <solid android:color="@color/gray_btn" />
        </shape>
    </item>
</selector> 
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/lvmain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:descendantFocusability="blocksDescendants"
        android:listSelector="@drawable/selector_shape_btn_gray" />
</RelativeLayout>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dip"
    android:layout_height="match_parent"
   
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_shape_btn_blue"
        android:text="aadfasfadsf" />

</LinearLayout>
点击item的时候item变色,button变色(我希望button不变色) 点击button的时候item没有变色,不头痛变色
引用 3 楼 hjywyj 的回复:
Y你还是把demo贴出来吧,我和老八都试过没出现你说的情况。
  • 打赏
  • 举报
回复
引用 5 楼 q445697127 的回复:
老八试出来了,他说不知道怎么办
把demo贴出来看看。
android_Y 2014-03-21
  • 打赏
  • 举报
回复
引用 3 楼 hjywyj 的回复:
Y你还是把demo贴出来吧,我和老八都试过没出现你说的情况。
老八试出来了,他说不知道怎么办
tanrunj 2014-03-21
  • 打赏
  • 举报
回复
可以尝试加入这三个属性
android:clickable="false"
       	 	android:focusable="false"
       		android:focusableInTouchMode="false"
实在不行可以在OnItemClickListener里面进行强制修改
  • 打赏
  • 举报
回复
Y你还是把demo贴出来吧,我和老八都试过没出现你说的情况。
龙哥1997 2014-03-20
  • 打赏
  • 举报
回复
虽然 不懂 Selector 是什么东东 ,但我知道android 的事件 分发 以及拦截。 覆写 某些方法拦截事件。。。我也不知道如何拦截 。 忘了,android 如此博大精深。。。

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧