如何在启动自定义操作栏后选择ListView中的项目。
public class UserListActivityAdapter extends BaseAdapter {
String[] mNameList;
int[] imageID;
Context mContext;
LayoutInflater inflater;
boolean isLongPressed = false;
public UserListActivityAdapter(UserListAcitivty context,
String[] nameList,
int[] imageList) {
this.mContext = context;
this.imageID = imageList;
this.mNameList = nameList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mNameList.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class Holder {
TextView tv;
TextView desc;
ImageView img;
}
@Override
public View getView(int position,
View convertView,
ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.row_layout_user, parent, false);
holder.tv = (TextView) rowView.findViewById(R.id.desc);
holder.desc = (TextView) rowView.findViewById(R.id.name);
holder.img = (ImageView) rowView.findViewById(R.id.icon);
holder.desc.setText(mNameList[position]);
holder.tv.setText("Blub");
holder.img.setImageResource(imageID[position]);
return rowView;
}
当我点击一个项目时,它会改变背景但当我启动动作栏时它不再改变。
public class UserListAcitivty extends Activity implements
MultiChoiceModeListener {
private ListView mListView;
String[] list;
private int count;
UserListActivityAdapter user;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = new String[] { "Hans Schmidt", "Felix DieKatze", "Wolfgang Müller" };
int[] img = new int[] { R.drawable.ic_launcher, R.drawable.abs__ab_bottom_solid_dark_holo, R.drawable.icon };
setContentView(R.layout.userlistactivity_layout);
mListView = (ListView) findViewById(R.id.userList);
mListView.setEmptyView(findViewById(R.id.empty_userlist));
user = new UserListActivityAdapter(this, list, img);
mListView.setAdapter(user);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setMultiChoiceModeListener(this);
}
@Override
public boolean onPrepareActionMode(ActionMode mode,
Menu menu) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
@Override
public boolean onCreateActionMode(ActionMode mode,
Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.my_context_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position,
long id,
boolean checked) {
count = count + 1;
mode.setTitle(count + " User selected");
mListView.setItemChecked(position, checked);
}
}
这是视图的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Normal state. -->
<item android:drawable="@color/list_row_default_bg"
android:state_pressed="false"
android:state_selected="false"/>
<!-- pressed state. -->
<item android:drawable="@color/list_row_pressed_bg"
android:state_pressed="true"/>
<!-- Selected state. -->
<item android:drawable="@color/list_row_selected_bg"
android:state_selected="true"/>