我在Android列表视图中有一个消息列表。我希望在按下Long Click时从列表中删除消息。我已经在列表选择器中声明了所有状态,但仍然长按。长按后,列表中的特定项目不会保持聚焦或选择。我尝试设置view.setselected(true),这也不起作用。我做错了吗?
这是我的list_selector。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/row_item_image" />
<item android:state_pressed="true"
android:drawable="@drawable/row_item_hover_image" />
<item android:state_focused="true"
android:drawable="@drawable/row_item_hover_image" />
<item android:state_selected="true"
android:drawable="@drawable/row_item_hover_image" />
</selector>
这是我的听众界面:
listview.setOnItemLongClickListener(new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long arg3) {
// TODO Auto-generated method stub
//view.setSelected(true);
if(view.isPressed())
{
view.setSelected(true);
}
return false;
}
});
答案 0 :(得分:2)
您不需要实现OnItemLongClickListener,因为ListView本身具有您想要的功能。请在您的活动中尝试:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
editListAdapter.removeSelection();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.delete_action_mode, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_mode:
SparseBooleanArray selected = editListAdapter.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
User selectedItem = editListAdapter.getItem(selected.keyAt(i));
editListAdapter.remove(selectedItem);
}
}
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
boolean checked) {
int checkedCount = listView.getCheckedItemCount();
mode.setTitle(checkedCount + " selected");
editListAdapter.toggleSelection(position);
}
});
您的ArrayAdapter类将如下所示:
public class UserListAdapter extends ArrayAdapter<User> {
LayoutInflater inflater;
private int resId;
private TextView userPosition, userName, userLogin, userPassw, userStatus, userRole;
private SparseBooleanArray mSelectedItemsIds;
private List<User> userList;
public UserListAdapter(Context context, int resource, List<User> users) {
super(context, resource, users);
this.resId = resource;
this.inflater = LayoutInflater.from(context);
this.userList = users;
mSelectedItemsIds = new SparseBooleanArray();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
User user = getItem(position);
UserItemHelper helper;
if (convertView == null) {
convertView = inflater.inflate(resId, parent, false);
userPosition = (TextView) convertView.findViewById(R.id.user_item_position);
userName = (TextView) convertView.findViewById(R.id.user_item_name);
userLogin = (TextView) convertView.findViewById(R.id.user_item_login);
userPassw = (TextView) convertView.findViewById(R.id.user_item_passw);
userStatus = (TextView) convertView.findViewById(R.id.user_item_status);
userRole = (TextView) convertView.findViewById(R.id.user_item_role);
helper = new UserItemHelper(userPosition, userName, userLogin, userPassw, userStatus, userRole);
convertView.setTag(helper);
} else {
helper = (UserItemHelper)convertView.getTag();
}
helper.getPosition().setText(position + 1 + "");
helper.getName().setText(user.getName());
helper.getLogin().setText(user.getLogin());
helper.getPassw().setText(user.getPassw());
helper.getStatus().setText(user.getStatus() +"");
helper.getRole().setText(user.getRole()+"");
return convertView;
}
public ArrayList<String> getItemsName() {
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < getCount(); i++) {
names.add(getItem(i).getName().toLowerCase());
}
return names;
}
@Override
public void remove(User object) {
// TODO Auto-generated method stub
super.remove(object);
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
private void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
}
同时创建菜单xml。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/delete_mode"
android:icon="@drawable/delete"
android:title="@string/delete_text"/>
</menu>