2014-02-20 78 views
0

在實現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; 
} 
} 
+1

爲此,您應該實現ListView的onItemClick事件。 – Piyush

回答

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> 

我已經找到解決我的問題。現在我刪除第二的LinearLayout在cashsaleolddoclist.xml。而隨後listselector效果仍然工作。感謝您的所有答案

0

試試這個: 在LinearLayout中設置android:clickable="true"

相關問題