当在自定义适配器中实现linearlayout onclick侦听器时,自定义listview listSelector不起作用

时间:2014-02-20 07:32:21

标签: android android-listview onclicklistener custom-lists

在PrdDocListAdapter中实现viewHolder._linearLayout.setOnClickListener之前,列表视图选中的行颜色更改事件是有效的。但是在创建onclick侦听器之后,选定的行颜色更改事件不起作用。请为我清楚。

这是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;
}
}

2 个答案:

答案 0 :(得分:0)

试试这个: 在LinearLayout中设置android:clickable="true"

答案 1 :(得分:0)

我的自定义列表视图布局(cashsaleolddoclist.xml)是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:background="@drawable/listview_selector" android:clickable="true" android:id="@+id/linlayDocnoList" 
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout android:orientation="horizontal" android:background="@color/White" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:textSize="14.0dip" android:textColor="#ff000000" android:id="@+id/txtOldDocNo" android:padding="5.0dip"  android:layout_marginLeft="2.0dip" android:layout_width="160.0dip" android:layout_height="wrap_content" android:text="" android:gravity="center" android:textAlignment="gravity" />
    <TextView android:textSize="14.0dip" android:textColor="#ff000000" android:id="@+id/txtOldDocDate" android:padding="5.0dip" android:layout_width="140.0dip"  android:layout_marginLeft="3.0dip"  android:layout_height="wrap_content" android:text="" android:gravity="center" android:textAlignment="gravity"/>
    <TextView android:textSize="14.0dip" android:textColor="#ff000000" android:id="@+id/txtOldDocCancelFlag" android:padding="5.0dip" android:layout_width="70.0dip"  android:layout_marginLeft="3.0dip"  android:layout_height="wrap_content" android:text="" android:gravity="center" android:textAlignment="gravity"/>
    <TextView android:textSize="14.0dip" android:textColor="#ff000000" android:id="@+id/txtOldDocTable" android:padding="5.0dip" android:layout_width="fill_parent"  android:layout_height="wrap_content" android:text="" android:gravity="center" android:textAlignment="gravity"/>                
</LinearLayout>
<View android:background="#ff000000" android:layout_width="wrap_content" android:layout_height="1.0dip" />
</LinearLayout>

我找到了解决方案我的问题。现在我在cashsaleolddoclist.xml中删除了第二个linearlayout。然后listselector效果仍然有效。感谢您的所有答案