listview中的item的监听分解

本文介绍如何在ListView中实现子项的点击监听,包括展示详细信息和跳转等功能,通过设置特定属性避免监听冲突。

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

前段时间项目中需要用到list中item里面的子控件实现不同的监听效果,并且和item整体监听效果不一致(点击item能展开,显示详情,加载图片,跳转页面等);并且不想使用recyclerview。在网上查找了很多,都是写的零零散散,不怎么完全,理解也不是很多,现在整理一下。

本例中就实现点开能展示详情和跳转。PS:其余的加载图片等实现方法同等。

首先是我们本例中需要实现的效果:


点击编号1(下面称为布局1)的位置展示2(下面称为布局2),点击布局2跳转界面(假若需要实现其他功能-如查看item中的图片,可继续将需要查看图片的view控件当作另外的板块)。

具体布局1代码块:

    <RelativeLayout
        android:focusable="false"
        android:id="@+id/new_ermcp_task_rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="3dp" >

        <RelativeLayout
            android:id="@+id/new_ermcp_task_rl1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/new_ermcp_task_rl1"
            android:paddingBottom="3dp" >

            <LinearLayout
                android:id="@+id/ll1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp" >

                <TextView
                    android:id="@+id/tv_ermcp_task_type_title_number"
                    android:layout_width="30dp"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:text="01"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/tv_ermcp_task_type_title"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:text="工单类型 "
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/tv_ermcp_task_type"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:text="抢险"
                    android:textColor="#FF0000"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/tv_ermcp_task_chuangjianshijian"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:paddingRight="25dp"
                    android:text="2016-09-01 11:59:22"
                    android:textColor="#666666"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_123"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ll1"
                android:paddingLeft="40dp" >

                <TextView
                    android:id="@+id/tv_ermcp_task_type2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:paddingRight="5dp"
                    android:text="抢险进行中"
                    android:textColor="#FF0000"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/tv_ermcp_task_chuangjianshijian2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.24"
                    android:gravity="right"
                    android:paddingRight="25dp"
                    android:text="王惠组"
                    android:textColor="#666666"
                    android:textSize="14sp" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="5dp" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fanyejiantou" />
        </RelativeLayout>

        <TextView
            android:id="@+id/ermcp_tv_fengexian"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_below="@+id/new_ermcp_task_rl1"
            android:layout_marginTop="5dp"
            android:background="#F3F3F3" />
    </RelativeLayout>
上述界面中主要注意的代码(其余的就是布局):

<strong>android:focusable="false"</strong>
此代码相当于是拦截了此item中1板块的父控件(即整个item)的监听事件,防止后续写入的监听事件和item的监听事件冲突,以达到其他监听的效果。

其余的代码和布局2中的代码就是普通的布局代码。


接下来主要的代码就在adapter中(当然考虑是案例,代码略有缩减)

监听方法在自定义adapter中的getView方法中编写

@Override
public View getView(int position, View convertView, ViewGroup parent) {}

首先获取布局1,布局2:

holder.new_ermcp_task_rl = (RelativeLayout) convertView.findViewById(R.id.new_ermcp_task_rl);// 布局1
holder.new_ermcp_task_item_rl = (RelativeLayout) convertView.findViewById(R.id.new_ermcp_task_item_rl);// 布局2

然后在此重写1的监听方法(重点):

当然我们在这里需要做一个判断:判断布局2是否为显示状态:

<strong>// 监听设置
holder.new_ermcp_task_rl.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View v) {
		if (holder.new_ermcp_task_item_rl.getVisibility() == View.VISIBLE) {
			holder.new_ermcp_task_item_rl.setVisibility(View.GONE);

		} else if (holder.new_ermcp_task_item_rl.getVisibility() == View.GONE) {
			holder.new_ermcp_task_item_rl.setVisibility(View.VISIBLE);

		}
	}
});</strong>

至此我们的adapter中主要的代码就算完成了。


然后我们在我们item监听方法里面设置监听就可以了:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
<span style="white-space:pre">	</span>// 跳转或者其他监听方法
}

上述就实现了listview中item中子布局监听方法拆分的效果。如果需要添加其他监听同理(可以精细化至每一个view控件)。


类似展示详情-我们如果点击了一个item1展开布局2之后没有再次点击隐藏,然后点击其他item2展示布局2,同时实现item1自动隐藏布局2,我们只需要在adapter中添加:一个辨别的标识。

下列来自网络其他收集:具体出处忘了,看到的作者望谅解:

1.adapter中添加数组来标识item是否展开,并初始化赋值:

// 这里定义一个数组,来识别item是否被选中
public int first[];
public DaibanAdapter(List<OrderModel> list, Context context) {
<span style="white-space:pre">	</span>super();
<span style="white-space:pre">	</span>this.list = list;
<span style="white-space:pre">	</span>// 这里初始化数组
<span style="white-space:pre">	</span>first = new int[list.size()];
<span style="white-space:pre">	</span>for (int i = 0; i < list.size(); i++) {
<span style="white-space:pre">		</span>first[i] = 0; // 0 标识未展开详情(展示布局2)
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>this.mInflater = LayoutInflater.from(context);
}
2.getView()方法中:

// 在这里进行判断,如果是0,代表没被选中,如果是1,代表被选中
if (first[position] == 0) {
	holder.new_ermcp_task_item_rl.setVisibility(View.GONE);

} else {
	holder.new_ermcp_task_item_rl.setVisibility(View.VISIBLE);

}
3.item的监听方法中:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	// 在这里进行改变adapter里面的first数组中的值,这里是重点哈
	if (daibanAdapter.first[position] == 0) {
		aAdapter.first[position] = 1;
	} else {
		adapter.first[position] = 0;
	}
	// 更新ui
	adapter.notifyDataSetChanged();		
}
以上三个步骤就实现了item1中显示布局2的时候,点击item2展示布局2,item1自动隐藏布局2的效果。

新人整理而来,有错误之处望指出。








编写一个Android应用,其中包含一个列表框(ListView)。列表框应显示一系列项目,例如“项目1”、“项目2”和“项目3”。当列表中的项目被点击时,应显示一个Toast消息,显示被点击项目的文本。 MainActivity.java import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private ListView listView; private String[] items = {"项目1", "项目2", "项目3"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.my_listview); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String item = items[position]; Toast.makeText(MainActivity.this, "你点击了:" + item, Toast.LENGTH_SHORT).show(); } }); } } activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/my_listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 修改建议: 1添加更多列表项到items数组中。 2更改列表项的布局(例如使用自定义的列表项布局)。 3尝试在点击事件中执行其他操作,如导航到另一个Activity或更新UI。 4调整ListView的高度,比如设置为wrap_content或指定具体的dp值。(给出详细步骤)
最新发布
06-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值