繁体   English   中英

在设备方向更改期间如何在上下文动作栏中保持突出显示的ListView项目及其状态

[英]How to Keep Highlighted ListView Item and Its Status in the Contextual Action Bar during Device Orientation Change

我实现了将其选择模式设置为CHOICE_MODE_MULTIPLE_MODAL的列表视图。 这是一些代码:

listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            private int check = 0;

            @Override
            public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode arg0) {
                // TODO Auto-generated method stub
                adapter.clearSelection();
            }

            @Override
            public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
                // TODO Auto-generated method stub

                check = 0;

                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.context_menu, arg1);
                return true;
            }

            @Override
            public boolean onActionItemClicked(final ActionMode arg0,
                    MenuItem arg1) {
                // TODO Auto-generated method stub
                if ((PinPad.mMediaPlayer != null)
                        && (!PinPad.mMediaPlayer.isPlaying())) {
                    switch (arg1.getItemId()) {
                    case R.id.item_delete:

                        LayoutInflater inflater = getLayoutInflater();
                        View dialogLayout = inflater.inflate(
                                R.layout.delete_confirmation, null);
                        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                MainListActivity.this);
                        alertDialogBuilder.setView(dialogLayout);

                        final AlertDialog confirmationDialog = alertDialogBuilder
                                .create();
                        confirmationDialog.show();

                        Button buttonNo = (Button) dialogLayout
                                .findViewById(R.id.buttonNo);
                        Button buttonYes = (Button) dialogLayout
                                .findViewById(R.id.buttonYes);

                        buttonNo.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                // TODO Auto-generated method stub
                                weGotTheTime.clear();
                                weGotTheType.clear();
                                weGotTheDays.clear();
                                weGotTheRings.clear();

                                weGotTheIDs.clear();

                                confirmationDialog.dismiss();
                                adapter.clearSelection();
                                adapter.notifyDataSetChanged();
                                refreshListView();
                                check = 0;
                                arg0.finish();
                            }
                        });

                        buttonYes
                                .setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        // TODO Auto-generated method stub
                                        databaseData.open();
                                        databaseData.deleteEntries(
                                                weGotTheTime, weGotTheType,
                                                weGotTheDays);
                                        databaseData.close();

                                        new AlarmTask(MainListActivity.this,
                                                weGotTheIDs, weGotTheDays,
                                                weGotTheRings, weGotTheType,
                                                weGotTheTime).cancelAl();

                                        weGotTheTime.clear();
                                        weGotTheType.clear();
                                        weGotTheDays.clear();
                                        weGotTheRings.clear();

                                        weGotTheIDs.clear();

                                        confirmationDialog.dismiss();
                                        adapter.clearSelection();
                                        adapter.notifyDataSetChanged();
                                        refreshListView();
                                        check = 0;
                                        arg0.finish();
                                    }
                                });
                        break;
                    }

                    return true;
                } else if (PinPad.mMediaPlayer == null) {
                    switch (arg1.getItemId()) {
                    case R.id.item_delete:

                        LayoutInflater inflater = getLayoutInflater();
                        View dialogLayout = inflater.inflate(
                                R.layout.delete_confirmation, null);
                        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                MainListActivity.this);
                        alertDialogBuilder.setView(dialogLayout);

                        final AlertDialog confirmationDialog = alertDialogBuilder
                                .create();
                        confirmationDialog.show();

                        Button buttonNo = (Button) dialogLayout
                                .findViewById(R.id.buttonNo);
                        Button buttonYes = (Button) dialogLayout
                                .findViewById(R.id.buttonYes);

                        buttonNo.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                // TODO Auto-generated method stub
                                weGotTheTime.clear();
                                weGotTheType.clear();
                                weGotTheDays.clear();
                                weGotTheRings.clear();

                                weGotTheIDs.clear();

                                confirmationDialog.dismiss();
                                adapter.clearSelection();
                                adapter.notifyDataSetChanged();
                                refreshListView();
                                check = 0;
                                arg0.finish();
                            }
                        });

                        buttonYes
                                .setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        // TODO Auto-generated method stub
                                        databaseData.open();
                                        databaseData.deleteEntries(
                                                weGotTheTime, weGotTheType,
                                                weGotTheDays);
                                        databaseData.close();

                                        new AlarmTask(MainListActivity.this,
                                                weGotTheIDs, weGotTheDays,
                                                weGotTheRings, weGotTheType,
                                                weGotTheTime).cancelAl();

                                        weGotTheTime.clear();
                                        weGotTheType.clear();
                                        weGotTheDays.clear();
                                        weGotTheRings.clear();

                                        weGotTheIDs.clear();

                                        confirmationDialog.dismiss();
                                        adapter.clearSelection();
                                        adapter.notifyDataSetChanged();
                                        refreshListView();
                                        check = 0;
                                        arg0.finish();
                                    }
                                });
                        break;
                    }

                    return true;
                } else {
                    Toast.makeText(
                            getApplicationContext(),
                            "Cannot delete alarm(s). Make sure no alarm is currently ringing.",
                            Toast.LENGTH_SHORT).show();
                    adapter.clearSelection();
                    check = 0;
                    arg0.finish();
                    return true;
                }
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode,
                    int position, long id, boolean checked) {
                // TODO Auto-generated method stub
                td = results.get(position).getDays();

                if (checked) {

                    check++;
                    adapter.setNewSelection(position, checked);
                    weGotTheTime.add(results.get(position).getTime());
                    weGotTheType.add(results.get(position).getType());
                    weGotTheDays.add(results.get(position).getDays());
                    weGotTheIDs.add(results.get(position).getID());
                    weGotTheRings.add(results.get(position).getTitle());
                } else {
                    check--;
                    adapter.removeSelection(position);
                    weGotTheTime.remove(results.get(position).getTime());
                    weGotTheType.remove(results.get(position).getType());
                    weGotTheDays.remove(results.get(position).getDays());
                    weGotTheIDs.remove(results.get(position).getID());
                    weGotTheRings.remove(results.get(position).getTitle());
                }

                mode.setTitle(check + " selected");
            }
        });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                listView.setItemChecked(arg2, !adapter.isPositionChecked(arg2));
                vibrator.vibrate(50);
                return false;
            }
        });

效果很好,只要长按列表视图项即可将其突出显示,并且效果很好。 但是,一旦我将设备的方向从纵向更改为横向,反之亦然,突出显示就消失了,并且上下文操作栏仍在显示,但是未显示任何内容。 有什么帮助吗? 我试过制作选择器和可绘制对象,并以这种方式手动进行操作,但仍然没有运气。 我已经在这个问题上花费了数周的时间,我想知道那里是否有人知道如何解决它。

谢谢。

看看是否可以使用此帮助:

android:configChanges =“ orientation | keyboardHidden”

有关完整实现的更多信息,请参见:

http://developer.android.com/guide/topics/resources/runtime-changes.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM