在實現viewHolder._linearLayout.setOnClickListener之前在PrdDocListAdapter中,listview所選行顏色變化事件是工作。但創建onclick監聽器後,選中的行顏色更改事件不起作用。請爲我清楚。當在自定義適配器中實現linearlayout onclick監聽器時,自定義listview listSelector不工作
這是cashsaleolddoc.xml的一部分
<ListView android:id="@+id/lvlOldDoc" android:fadingEdge="none"
android:layout_width="fill_parent" android:layout_height="200.0dip"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@drawable/listview_selector"
android:drawSelectorOnTop="true"
android:choiceMode="singleChoice"
android:layout_weight="1.0"
/>
這是listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/Lime_Green" /> <!-- @drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@color/Lime_Green" /> <!-- @drawable/tab_press -->
</selector>
這是PrdDocListAdapter
public class PrdDocListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater inflater;
private List<CashSaleDocInfo> _CashSaleDocInfo;
public PrdDocListAdapter(Context paramContext,
List<CashSaleDocInfo> paramList1) {
this.mContext = paramContext;
this._CashSaleDocInfo = paramList1;
}
@Override
public int getCount() {
return _CashSaleDocInfo.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parentView) {
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.cashsaleolddoclist, null);
TextView txtOldDocNo = (TextView) convertView
.findViewById(R.id.txtOldDocNo);
TextView txtOldDocDate = (TextView) convertView
.findViewById(R.id.txtOldDocDate);
TextView txtOldDocCancelFlag = (TextView) convertView
.findViewById(R.id.txtOldDocCancelFlag);
TextView txtOldDocTable = (TextView) convertView
.findViewById(R.id.txtOldDocTable);
// EditText txtSelectDocNo = (EditText) convertView
// .findViewById(R.id.txtSelectDocNo);
viewHolder = new ViewHolder();
viewHolder.txtDocNo = txtOldDocNo;
viewHolder.txtDocDate = txtOldDocDate;
viewHolder.txtCancelFlag = txtOldDocCancelFlag;
viewHolder.txtTableNo = txtOldDocTable;
// viewHolder.txtSelectDocNo = txtSelectDocNo;
viewHolder._linearLayout = (LinearLayout) convertView
.findViewById(R.id.linlayDocnoList);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtDocNo.setText(this._CashSaleDocInfo.get(position)
.getDOCUMENT_NO());
viewHolder.txtDocDate.setText(dateFormat.format(this._CashSaleDocInfo
.get(position).getDOCUMENT_DATE()));
viewHolder.txtCancelFlag.setText(this._CashSaleDocInfo.get(position)
.getCANCEL_FLAG());
viewHolder.txtTableNo.setText(this._CashSaleDocInfo.get(position)
.getSALEMAN_CODE());
viewHolder._linearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String a = _CashSaleDocInfo.get(position).getDOCUMENT_NO()
.toString();
MainActivity.setSelectDocNo(a);
}
});
return convertView;
}
public static class ViewHolder {
TextView txtDocNo;
TextView txtDocDate;
TextView txtCancelFlag;
TextView txtTableNo;
EditText txtSelectDocNo;
LinearLayout _linearLayout;
}
}
爲此,您應該實現ListView的onItemClick事件。 – Piyush