-3

我已经实现了 MultichoicemodeListener 并且它产生了一些奇怪的问题......我在列表中有 12 个项目...... 8 个可见,4 个在屏幕下方.. 所以这 4 个是隐藏的。当我从 8 个可见项目中长按时......该代码还从隐藏视图中更改了另一个的背景......其余功能正在工作。请帮助..我从过去 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);
}
4

1 回答 1

0

经过大量谷歌搜索并尝试多种布局解决方案......我终于找到了答案......但首先让我们了解问题及其原因:

原因:多个列表项被突出显示(选择工作正常/突出显示和选择是不同的东西),因为在滚动列表时回收 ListView 的视图。突出显示的视图在滚动时被重用,因此下一个可见视图也被突出显示。

解决方案:只需在style.xml中添加一个样式

<style name="activated" parent="AppTheme">
    <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>

现在转到您正在使用的自定义布局,将其放在根目录中,然后您就可以设置了。

style="@style/activated"

如果您想覆盖默认选择颜色,那么也可以通过定义新样式,其中包括用于激活模式的可绘制。

我唯一需要删除的是自定义边框才能正常工作。其余所有代码保持不变。

感谢 Jonathan727 的时间和努力。非常感谢。

于 2015-09-25T08:14:42.307 回答