需求如题目:Android listview中item部分区域添加点击事件,在一个界面显示了listview,但显示的内容分为上下两部分,分别是白色的背景和蓝色的背景,现在需要只点击蓝色的背景,才能跳转到其他界面,解决方式如下:
一开始想着是不是能在list item的布局给上层布局添加一个:
android:clickable="false"
android:focusable="false"
就是禁止点击,但试了试没有效果,后来师傅提醒我,我的这个listview使用的适配器是BaseAdapter可以在适配器里面拿到下层蓝色的Relativelayout的id,然后给这个布局添加点击事件,就可以实现部分区域响应事件,事实证明是非常可行的,贴出主要的代码如下:
// 开仓单适配器 public class OrderAdapter extends BaseAdapter { public OrderAdapter(Context myContext) { } public OrderAdapter(OrderFragment orderFragment) { } @Override public int getCount() { if (dataMap == null) { return orderVec.length; } return dataMap.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); <span style="font-size:24px;color:#ff0000;"><strong>convertView = getActivity().getLayoutInflater().inflate( R.layout.activity_order_item, null); initViewHolder(convertView, viewHolder, position);</strong></span>
//关键代码,获得下层蓝色的布局的id } else { viewHolder = (ViewHolder) convertView.getTag(); } refreshViewHolder(position, viewHolder); return convertView; } private void refreshViewHolder(int position, ViewHolder viewHolder) { 。。。。。。。。 } private void initViewHolder(View convertView, ViewHolder viewHolder, final int position) { <span style="font-size:18px;color:#ff0000;"><strong>viewHolder.modeifyorder = (RelativeLayout) convertView .findViewById(R.id.modeifyorder); viewHolder.modeifyorder.setOnClickListener(new OnClickListener() { </strong></span> @Override public void onClick(View v) { Map<String, Object> map = dataMap.get(position); long orderId = Long.parseLong(map.get(Orderid).toString()); String instStr = map.get(Instrument).toString(); DocCaptain.getInstance().setOrderTrade2Modify(instStr, orderId); getSelfActivity().showOrHideOrderTradeFragment(); } }); convertView.setTag(viewHolder); } private class ViewHolder { TextView orderid; TextView iFDStopPrice; <span style="font-size:18px;color:#ff0000;"><strong>RelativeLayout modeifyorder; </strong></span> } }