解决android ListView item中事件处理无法响应方法

本文讨论了在Android应用中使用ListView和Button时遇到的按钮无法响应问题,并提供了有效的解决方案。通过调整布局文件、点击事件监听和适配器配置,确保了按钮功能正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天做了一个微薄关注列表,需要对某个用户进行关注,获取Button和OnItemClickListener无法响应

以下是我个我解决方案:

主布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    style="@style/Global_Bg">
    
    <include layout="@layout/top_box" android:id="@+id/top_box"/>

    <cn.xxxxx.xxxx.xxxxxxListView
	android:id="@+id/fans_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:cacheColorHint="#00000000"
        android:divider="@drawable/lin"
        android:fadeScrollbars="true"/>
</LinearLayout>

item布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="80dip"
        android:layout_marginTop="5dip"
        android:descendantFocusability= "blocksDescendants" ><!-- 这个一定要设置-->
        
        <Button
            android:id="@+id/createfriendship"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:layout_alignBottom="@+id/friendsCount"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dip"
            android:background="@drawable/button_shape1"
            style="@style/button_text"
            android:textStyle="bold"
            android:focusable = "false"/><!-- 这个一定要设置-->

    </RelativeLayout>

</LinearLayout>

Adapter代码

@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		//------省略
		
		
		fansView.getBtnCreateFriendship().setOnClickListener(new FriendShips(position));
		
		
		return convertView;
	}
	
	//只要看这里就可以了
	class FriendShips implements OnClickListener{
		private  int position;
		
		FriendShips(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			int vid=v.getId();
			if(vid==fansView.getBtnCreateFriendship().getId()){
				Toast.makeText(context, "position:"+position, 2000).show();
				fansView.getBtnCreateFriendship().setBackgroundResource(R.drawable.button_shape3);
			}
		}
	}


以上是我的解决方法,大家共同进步


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值