multichoicemodellistener 为单击选择多个项目
multichoicemodelistener selecting multiple items for single click
我已经实现了 MultichoicemodeListener 并且它产生了一些奇怪的问题...我在列表中有 12 个项目...8 个是可见的,4 个在屏幕下方..所以这 4 个是隐藏的。当我从 8 长按时可见项目...该代码还从隐藏视图中更改了另一个项目的背景......其余功能是working.Plz帮助..我从过去2天开始就被困住了。
MyCusrsorAdaptor customAdaptor;
private Cursor mCursor;
private ListView lv01;
View view;
int itemsSelectedCount=0;
SelectedItems = new SparseBooleanArray();
lv01.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv01.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
itemsSelectedCount = toggleSelection(position, checked);
mode.setTitle(itemsSelectedCount + " Messages Selected");
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_multichoice, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_msg: {
DeleteSelectedRows();
}
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
SelectedItems.clear();
SelectedItems = new SparseBooleanArray();
lv01.clearChoices();
recreate();
}
});
}
private void DeleteSelectedRows()
{
for(int k =0; k< SelectedItems.size();k++)
{
Cursor curitem = (Cursor) lv01.getItemAtPosition(SelectedItems.keyAt(k));
String rowID = curitem.getString(curitem.getColumnIndex(dbHelper.COL_ROWID));
String whereclause = dbHelper.COL_ROWID+"=?";
db.delete(dbHelper.TABLE_NAME3,whereclause,new String[]{rowID});
Log.d(TAG, "Delteted ROWID is --> "+rowID);
}
UpdateAlarms();
recreate();
}
private int toggleSelection(int position, boolean checked)
{
RelativeLayout relativeLayout = (RelativeLayout)lv01.getChildAt(position);
if(checked==true)
{ SelectedItems.put(position,checked);
relativeLayout.setBackground(getResources().getDrawable(R.drawable.border_multichoice));
}
else{
SelectedItems.delete(position);
relativeLayout.setBackground(getResources().getDrawable(R.drawable.borderedittask));
}
return SelectedItems.size();
}
请在下面找到光标适配器的代码
public class MyCusrsorAdaptor extends CursorAdapter
{
private LayoutInflater curinf;
private MyDBHelper dbHelper;
private static final String TAG="OK OK OK OK OK";
public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date date;
public MyCusrsorAdaptor(Context context, Cursor c, int flags) {
super(context, c, flags);
curinf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView title = (TextView) view.findViewById(R.id.title);
String titleName = cursor.getString(cursor.getColumnIndex(dbHelper.COL_USERNAME));
title.setFocusable(false);
title.setFocusableInTouchMode(false);
ImageView imv = (ImageView) view.findViewById(R.id.list_image);
imv.setFocusable(false);
imv.setFocusableInTouchMode(false);
TextView artist1 = (TextView) view.findViewById(R.id.artist1);
TextView artist2 = (TextView) view.findViewById(R.id.artist2);
artist1.setFocusable(false);
artist1.setFocusableInTouchMode(false);
artist2.setFocusable(false);
artist2.setFocusableInTouchMode(false);
String userid = cursor.getString(cursor.getColumnIndex(dbHelper.COL_1));
String userRel = cursor.getString(cursor.getColumnIndex(dbHelper.COL_2));
String dur = cursor.getString(cursor.getColumnIndex(dbHelper.COL_3));
String mstyp = cursor.getString(cursor.getColumnIndex(dbHelper.COL_4));
String msg = cursor.getString(cursor.getColumnIndex(dbHelper.COL_5));
try {
date = dateFormat.parse(cursor.getString(cursor.getColumnIndex(dbHelper.COL_6)));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
artist1.setText("Lets Rock on: "+ PrintTypeDateFormatter(date));
artist2.setText(msg);
title.setText(titleName+" ("+userid+" )");
}
private String PrintTypeDateFormatter(Date date)
{
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE dd MMM yyyy hh:mm a");
String toPrint = dateFormat2.format(date).toString();
android.util.Log.d(TAG, toPrint);
return toPrint;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return curinf.inflate(R.layout.custom_layout_messages_type,parent,false);
}
经过大量谷歌搜索并尝试多种布局解决方案....我终于找到了答案....但首先让我们了解问题及其原因:
原因:多个列表项被突出显示(选择工作正常/突出显示和选择是不同的东西)因为在滚动时回收 ListView 的视图 list.The 突出显示的视图在滚动时被重新使用,因此下一个可见视图也被突出显示。
解决方法:在style.xml
中添加样式即可
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
现在转到您正在使用的自定义布局,将其放入根目录即可。
style="@style/activated"
如果你想覆盖默认的选择颜色,那么也可以通过定义新的样式,其中包括用于激活模式的可绘制对象。
我唯一需要删除的是自定义边框才能正常工作。其余所有代码保持不变。
非常感谢 Jonathan727 付出的时间和精力。非常感谢。
我已经实现了 MultichoicemodeListener 并且它产生了一些奇怪的问题...我在列表中有 12 个项目...8 个是可见的,4 个在屏幕下方..所以这 4 个是隐藏的。当我从 8 长按时可见项目...该代码还从隐藏视图中更改了另一个项目的背景......其余功能是working.Plz帮助..我从过去2天开始就被困住了。
MyCusrsorAdaptor customAdaptor;
private Cursor mCursor;
private ListView lv01;
View view;
int itemsSelectedCount=0;
SelectedItems = new SparseBooleanArray();
lv01.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv01.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
itemsSelectedCount = toggleSelection(position, checked);
mode.setTitle(itemsSelectedCount + " Messages Selected");
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_multichoice, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_msg: {
DeleteSelectedRows();
}
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
SelectedItems.clear();
SelectedItems = new SparseBooleanArray();
lv01.clearChoices();
recreate();
}
});
}
private void DeleteSelectedRows()
{
for(int k =0; k< SelectedItems.size();k++)
{
Cursor curitem = (Cursor) lv01.getItemAtPosition(SelectedItems.keyAt(k));
String rowID = curitem.getString(curitem.getColumnIndex(dbHelper.COL_ROWID));
String whereclause = dbHelper.COL_ROWID+"=?";
db.delete(dbHelper.TABLE_NAME3,whereclause,new String[]{rowID});
Log.d(TAG, "Delteted ROWID is --> "+rowID);
}
UpdateAlarms();
recreate();
}
private int toggleSelection(int position, boolean checked)
{
RelativeLayout relativeLayout = (RelativeLayout)lv01.getChildAt(position);
if(checked==true)
{ SelectedItems.put(position,checked);
relativeLayout.setBackground(getResources().getDrawable(R.drawable.border_multichoice));
}
else{
SelectedItems.delete(position);
relativeLayout.setBackground(getResources().getDrawable(R.drawable.borderedittask));
}
return SelectedItems.size();
}
请在下面找到光标适配器的代码
public class MyCusrsorAdaptor extends CursorAdapter
{
private LayoutInflater curinf;
private MyDBHelper dbHelper;
private static final String TAG="OK OK OK OK OK";
public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date date;
public MyCusrsorAdaptor(Context context, Cursor c, int flags) {
super(context, c, flags);
curinf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView title = (TextView) view.findViewById(R.id.title);
String titleName = cursor.getString(cursor.getColumnIndex(dbHelper.COL_USERNAME));
title.setFocusable(false);
title.setFocusableInTouchMode(false);
ImageView imv = (ImageView) view.findViewById(R.id.list_image);
imv.setFocusable(false);
imv.setFocusableInTouchMode(false);
TextView artist1 = (TextView) view.findViewById(R.id.artist1);
TextView artist2 = (TextView) view.findViewById(R.id.artist2);
artist1.setFocusable(false);
artist1.setFocusableInTouchMode(false);
artist2.setFocusable(false);
artist2.setFocusableInTouchMode(false);
String userid = cursor.getString(cursor.getColumnIndex(dbHelper.COL_1));
String userRel = cursor.getString(cursor.getColumnIndex(dbHelper.COL_2));
String dur = cursor.getString(cursor.getColumnIndex(dbHelper.COL_3));
String mstyp = cursor.getString(cursor.getColumnIndex(dbHelper.COL_4));
String msg = cursor.getString(cursor.getColumnIndex(dbHelper.COL_5));
try {
date = dateFormat.parse(cursor.getString(cursor.getColumnIndex(dbHelper.COL_6)));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
artist1.setText("Lets Rock on: "+ PrintTypeDateFormatter(date));
artist2.setText(msg);
title.setText(titleName+" ("+userid+" )");
}
private String PrintTypeDateFormatter(Date date)
{
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE dd MMM yyyy hh:mm a");
String toPrint = dateFormat2.format(date).toString();
android.util.Log.d(TAG, toPrint);
return toPrint;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return curinf.inflate(R.layout.custom_layout_messages_type,parent,false);
}
经过大量谷歌搜索并尝试多种布局解决方案....我终于找到了答案....但首先让我们了解问题及其原因:
原因:多个列表项被突出显示(选择工作正常/突出显示和选择是不同的东西)因为在滚动时回收 ListView 的视图 list.The 突出显示的视图在滚动时被重新使用,因此下一个可见视图也被突出显示。
解决方法:在style.xml
中添加样式即可<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
现在转到您正在使用的自定义布局,将其放入根目录即可。
style="@style/activated"
如果你想覆盖默认的选择颜色,那么也可以通过定义新的样式,其中包括用于激活模式的可绘制对象。
我唯一需要删除的是自定义边框才能正常工作。其余所有代码保持不变。
非常感谢 Jonathan727 付出的时间和精力。非常感谢。