变化的ListView choiceMode从singleChoice到multipleChoiceModalchoiceMode、ListView、multipleChoiceModal、singl

2023-09-06 00:22:04 作者:鹤归

我有一个ListView通常是 singleChoice choiceMode 。当用户长presses上的一个项目,我想进入的操作模式,允许选择多个项目,使他们能够在任何选定的项目执行的操作。

我能够配置的ListView ,以便它在 singleChoice 模式下,用户能够选择列表项显示的细节片段旁边并有列表项本身显示在其激活状态。

我也能够配置的ListView ,以便它在 multipleChoiceModal choiceMode 并在项目执行长preSS启动的操作模式,允许多个选择,但现在的ListView将不允许在正常模式下的单一选择(无操作模式)。

我怎么能有一个ListView是在 singleChoice 模式,然后将其转换为 multipleChoiceModal 模式,当项目漫漫其修远pressed?

这是最接近我已经能够拿出:

设置ListView控件为 singleChoice 模式 设置ListView的 OnItemLongClickListener ,并在监听器: 设置ListView的 OnItemLongClickListener 设置ListView的 choiceMode multipleChoiceModal 呼叫 view.performClick()上那是很久pressed的项目。

该方法有几个问题。

的操作模式没有启动,直到第二次在项目上我长的preSS。 当我打电话 getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE); onDestroyActionMode 我收到了 java.lang.StackOverflowError ,因为这种方法最终试图摧毁的操作模式,以及(但我们还没有从破坏返回)。 解决方案

我在我的计划之一用这种

我们 ListView.CHOICE_MODE_MULTIPLE_MODAL 然后 lv.setMultiChoiceModeListener(新ModeCallBack());

 公共类ModeCallBack实现ListView.MultiChoiceModeListener {

    查看mSelectView;
    TextView的mSelectedCount;
    ArrayList的<龙> mCheckedItems;

    @覆盖
    公共布尔onActionItemClicked(ActionMode模式,菜单项项){
        共享preferences preF = preferenceManager.getDefaultShared preferences(getActivity());
        共享preferences.Editor编辑= pref.edit();

        如果(item.getItemId()== R.id.bowler_delete){

            的for(int i = 0; I< mCheckedItems.size();我++){
                长的id = mCheckedItems.get(ⅰ);

                。getActivity()getContentResolver()删除(BowlersDB.CONTENT_URI,BowlersDB.ID +=+ ID,NULL);
            }
        }否则,如果(item.getItemId()== R.id.bowler_add_ball){
            如果(mCheckedItems.size()→1){
                Toast.makeText(getActivity(),只能加保龄球一个投球手在同一时间,Toast.LENGTH_SHORT).show();
            }其他{
                。edit.putLong(preferences.BOWLER_SELECTED_FOR_BALL,mCheckedItems.get(0))提交();

                ListFragment LF =新ManufacturersList();
                FragmentTransaction英尺;
                FT = getFragmentManager()的BeginTransaction()。
                ft.replace(R.id.frameOne,LF).addToBackStack(空).commit();
                //mRemover.rFragment();
            }
        }否则,如果(item.getItemId()== R.id.add_bowler_to_team){
            的for(int i = 0; I< mCheckedItems.size();我++){

                长的id = mCheckedItems.get(ⅰ);
                ContentValues​​值=新ContentValues​​();
                values​​.put(TeamBowlers.BOWLER_ID,ID);
                values​​.put(TeamBowlers.TEAM_ID,pref.getLong(preferences.TEAM_SELECTED,1));
                。getActivity()getContentResolver()插入(TeamBowlers.CONTENT_URI,价值观);

            }
            FragmentManager FM = getFragmentManager();
            fm.popBackStack();
        }
        mode.finish();
        返回true;
    }

    @覆盖
    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
        MenuInflater膨胀= getActivity()getMenuInflater()。
        如果(fromTeam){
            inflate.inflate(R.menu.bowlers_team_action_menu,菜单);
        }其他{
            inflate.inflate(R.menu.bowler_action_menu,菜单);
        }
        如果(mSelectView == NULL){
            。mSelectView =(ViewGroup中)LayoutInflater.from(getActivity())膨胀(R.layout.select_count_layout,NULL);

            mSelectedCount =(TextView中)mSelectView.findViewById(R.id.count_tv);

        }
        如果(mCheckedItems == NULL){
            mCheckedItems =新的ArrayList<龙>();
        }
        mode.setCustomView(mSelectView);
        返回true;
    }

    @覆盖
    公共无效onDestroyActionMode(ActionMode模式){
        mCheckedItems = NULL;

    }

    @覆盖
    在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
        如果(mSelectView == NULL){
            。mSelectView =(ViewGroup中)LayoutInflater.from(getActivity())膨胀(R.layout.select_count_layout,NULL);

            mSelectedCount =(TextView中)mSelectView.findViewById(R.id.count_tv);
        }

        如果(mCheckedItems == NULL){
            mCheckedItems =新的ArrayList<龙>();
        }
        返回true;
    }

    @覆盖
    公共无效onItemCheckedStateChanged(ActionMode模式,INT位置,长的ID,布尔检查){

        最终诠释计数= lv.getCheckedItemCount();
        mSelectedCount.setText(将String.valueOf(计数));
        如果(检查){
            mCheckedItems.add(ID);
        }其他{
            mCheckedItems.remove(ID);
        }
    }

}
 
布局与控件 八 ListView知多少 下 ChoiceMode详解

这允许单一选择单列表视图点击并长按多重选择。这是所有从ICS消息应用程序拉升,所以你可以浏览过

I have a ListView that normally is the singleChoice choiceMode. When the user long presses on an item, I want to enter an action mode that allows selecting multiple items so they can perform an action on any selected items.

I am able to configure the ListView so that it is in singleChoice mode and the user is able to select list items to display a details fragment next to it and have the list item itself shown in its activated state.

I am also able to configure the ListView so that it is in the multipleChoiceModal choiceMode and performing a long press on an item starts the action mode and allows multiple selections, but now the ListView will not allow a single selection in the normal mode (no action mode).

How can I have a ListView that is in singleChoice mode and then transition it to multipleChoiceModal mode when an item is long pressed?

This is the closest I've been able to come up with:

set the ListView to singleChoice mode set the ListView's OnItemLongClickListener and in that listener:

set the ListView's OnItemLongClickListener to null set the ListView's choiceMode to multipleChoiceModal call view.performClick() on the item that was long pressed.

This approach has a couple problems.

The action mode isn't started until the second time I long press on an item. When I call getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); in onDestroyActionMode I get a java.lang.StackOverflowError because that method ends up trying to destroy the action mode as well (but we have no yet returned from the destroy).

解决方案

I used this in one of my programs

us the ListView.CHOICE_MODE_MULTIPLE_MODAL then lv.setMultiChoiceModeListener(new ModeCallBack());

    public class ModeCallBack implements ListView.MultiChoiceModeListener{

    View mSelectView;
    TextView mSelectedCount;
    ArrayList<Long> mCheckedItems;

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor edit = pref.edit();

        if(item.getItemId() == R.id.bowler_delete){

            for(int i=0; i<mCheckedItems.size(); i++){
                long id = mCheckedItems.get(i);

                getActivity().getContentResolver().delete(BowlersDB.CONTENT_URI,BowlersDB.ID+"="+id,null);
            }
        }else if(item.getItemId() == R.id.bowler_add_ball){
            if(mCheckedItems.size() > 1){
                Toast.makeText(getActivity(),"Can only add bowling balls to one bowler at a time",Toast.LENGTH_SHORT).show();
            }else{
                edit.putLong(Preferences.BOWLER_SELECTED_FOR_BALL,mCheckedItems.get(0)).commit();

                ListFragment lf = new ManufacturersList();
                FragmentTransaction ft;
                ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.frameOne, lf).addToBackStack(null).commit();
                //mRemover.rFragment();
            }
        }else if(item.getItemId() == R.id.add_bowler_to_team){
            for(int i=0; i<mCheckedItems.size(); i++){

                long id = mCheckedItems.get(i);
                ContentValues values = new ContentValues();
                values.put(TeamBowlers.BOWLER_ID,id);
                values.put(TeamBowlers.TEAM_ID,pref.getLong(Preferences.TEAM_SELECTED,1));
                getActivity().getContentResolver().insert(TeamBowlers.CONTENT_URI, values);

            }
            FragmentManager fm = getFragmentManager();
            fm.popBackStack();
        }
        mode.finish();
        return true;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflate = getActivity().getMenuInflater();
        if(fromTeam){
            inflate.inflate(R.menu.bowlers_team_action_menu, menu);
        }else{
            inflate.inflate(R.menu.bowler_action_menu, menu);
        }
        if(mSelectView == null){
            mSelectView = (ViewGroup)LayoutInflater.from(getActivity()).inflate(R.layout.select_count_layout,null);

            mSelectedCount = (TextView)mSelectView.findViewById(R.id.count_tv);

        }
        if(mCheckedItems == null){
            mCheckedItems = new ArrayList<Long>();
        }
        mode.setCustomView(mSelectView);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mCheckedItems = null;

    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        if(mSelectView == null){
            mSelectView = (ViewGroup)LayoutInflater.from(getActivity()).inflate(R.layout.select_count_layout,null);

            mSelectedCount = (TextView)mSelectView.findViewById(R.id.count_tv);
        }

        if(mCheckedItems == null){
            mCheckedItems = new ArrayList<Long>();
        }
        return true;
    }

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,long id, boolean checked) {         

        final int count = lv.getCheckedItemCount();
        mSelectedCount.setText(String.valueOf(count));
        if(checked){
            mCheckedItems.add(id);
        }else{
            mCheckedItems.remove(id);
        }
    }

}

this allows for single choice single listview click and long click multiple selection. This was all pulled from the ICS messaging app so you can browse that too