前段时间项目中需要用到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的效果。
新人整理而来,有错误之处望指出。