自己重写适配器,代码如下:
package com.example.base;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.example.bookfootdetail.R;
public class lvButtonAdapter extends BaseAdapter {
private class buttonViewHolder {
TextView footid;
TextView footname;
TextView footprice;
TextView footstate;
TextView ordertime;
TextView footnum;
Button delaction;
}
private LayoutInflater mInflater;
private buttonViewHolder holder;
private List<Map<String, String>> mAppList;
private Context mContext;
private String[] keyString;
private int[] valueViewID;
public lvButtonAdapter(Context c,List<Map<String, String>> appList, int resource, String[] from, int[] to) {
mAppList = appList;
mContext = c;
//取得xml里定义的view
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
keyString = new String[from.length];
valueViewID = new int[to.length];
System.arraycopy(from, 0, keyString, 0, from.length);
System.arraycopy(to, 0, valueViewID, 0, to.length);
}
@Override
public int getCount() {
return mAppList.size();
}
@Override
public Object getItem(int position) {
return mAppList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//如果convertview 是回收来的那么我们就不必创建新的holder对象,只需要把原来的绑定的holder取出加上新的数据就行了
if (convertView != null) {
holder = (buttonViewHolder) convertView.getTag();
} else {
//如果convertview 是第一次展示我们就创建新的Holder对象与之绑定,并在最后通过return convertview 返回
convertView = mInflater.inflate(R.layout.sublistdata, null);
holder = new buttonViewHolder();
holder.footid = (TextView) convertView.findViewById(R.id.footid);
holder.footname = (TextView) convertView.findViewById(R.id.footname);
holder.footprice = (TextView) convertView.findViewById(R.id.footprice);
holder.footstate = (TextView) convertView.findViewById(R.id.footstate);
holder.ordertime = (TextView) convertView.findViewById(R.id.ordertime);
holder.footnum = (TextView) convertView.findViewById(R.id.footnum);
holder.delaction = (Button) convertView.findViewById(R.id.doaction);
convertView.setTag(holder);
}
Map<String, String> appInfo = mAppList.get(position);
if (appInfo != null) {
String footid = (String) appInfo.get("编号");
String footname = (String) appInfo.get("菜名");
String footprice = (String) appInfo.get("单价");
String footstate = (String) appInfo.get("状态");
String ordertime = (String) appInfo.get("下单时间");
String footnum = (String) appInfo.get("数量");
holder.footid.setText(footid);
holder.footname.setText(footname);
holder.footprice.setText(footprice);
holder.footstate.setText(footstate);
holder.ordertime.setText(ordertime);
holder.footnum.setText(footnum);
holder.delaction.setOnClickListener(new lvButtonListener(position));
}
return convertView;
}
class lvButtonListener implements OnClickListener {
private int position;
lvButtonListener(int pos) {
position = pos;
}
@Override
public void onClick(View v) {
int vid = v.getId();
if (vid == holder.delaction.getId())
removeItem(position);
}
}
public void removeItem(int position) {
mAppList.remove(position);
this.notifyDataSetChanged();
}
}
点击item跳到详情页面:
1.在总体布局的RelativeLayout 中添加下面的属性:
android:descendantFocusability="blocksDescendants"
即就是:
<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:descendantFocusability="blocksDescendants"
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不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点。原因多半是由于在你自己定义的Item中存在诸如ImageButton,Button,CheckBox等子控件(也可以说是Button或者Checkable的子类控件),此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
通常我们用到的是第三种;
2.在button按钮中添加下面的属性:
android:focusable="false"
<Button
android:id="@+id/doaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="0.5"
android:layout_marginTop="25dp"
android:focusable="false"
android:text="@string/delete" />