我正在尝试将上下文操作模式添加到列表视图,但我在选择时遇到了一些问题,如果我做出List1.setSelection(position)
它不会选择任何东西,如果我List1.setItemChecked(position, true)
让它工作但它只会改变字体颜色很少,我希望它改变背景或更值得注意的东西,有没有办法检测选择并手动改变背景,或者我错过了什么?
名单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="false">
</ListView>
</RelativeLayout>
适配器:
public class ServicesRowAdapter extends ArrayAdapter<String[]> {
private final Activity context;
private final ArrayList<String[]> names;
static class ViewHolder {
public TextView Id;
public TextView Date;
public RelativeLayout statusbar,bglayout;
}
public ServicesRowAdapter(Activity context, ArrayList<String[]> names) {
super(context, R.layout.servicesrowlayout, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.servicesrowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Id = (TextView) rowView.findViewById(R.id.idlabel);
viewHolder.Date = (TextView) rowView.findViewById(R.id.datelabel);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.Date.setText(names.get(position)[2]);
holder.Id.setText(names.get(position)[1]);
return rowView;
}
}
使用布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/idlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:text="@+id/idlabel"
android:textSize="20dp"
android:width="70dp" >
</TextView>
<TextView
android:id="@+id/datelabel"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/datelabel"
android:textSize="20dp"
android:layout_marginLeft="90dp" >
</TextView>
</RelativeLayout