在片段上下文动作方式 - 如果接近不集中? [英] contextual action mode in fragment - close if not focused?

查看:271
本文介绍了在片段上下文动作方式 - 如果接近不集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现嵌套fragement上下文动作方式吧。这片段是一个视图寻呼机和视图寻呼机的组成部分,也是一个抽屉式导航栏中的片段组成部分。

我的问题:我要关闭上下文动作模式吧,如果片段没有更集中。所以,如果我通过视图寻呼机刷卡操作模式栏应该关闭。但是,如果使用嵌套片段的的onPause()方法,该方法不直接调用。通常它会等待,直到我刷着两三次......下面是一些图片:

在第二张照片,你可以看到,作用方式栏仍然存在。所以我的问题是:
在该方法中,我应该叫我的 actionModeBar.finish()方法,如果我离开的片段直接关闭的动作模式吧?

也许片段的code可以帮助你:

 公共类EditorFragment扩展片段{  私有静态最后弦乐KEY_POSITION =位置;
  ListView控件的ListView;
  私人布尔isMultipleList = FALSE;
  私人ActionMode acMode;
  私人INT counterChecked = 0;  私人ActionMode.Callback modeCallBack =新ActionMode.Callback(){    在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
           返回false;
       }      公共无效onDestroyActionMode(ActionMode模式){
          listView.clearChoices();
            的for(int i = 0; I< listView.getChildCount();我++)
                listView.setItemChecked(I,FALSE);
                listView.post(新的Runnable(){
                    @覆盖
                    公共无效的run(){
                        listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
                    }
                });
          isMultipleList = FALSE;
          counterChecked = 0;
          模式= NULL;
       }       公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
           mode.setTitle(1 Aufgabe);
           。mode.getMenuInflater()膨胀(R.menu.actionmode,菜单);
           返回true;
       }       公共布尔onActionItemClicked(ActionMode模式,菜单项项){
           开关(item.getItemId()){
           案例R.id.actionmode_delete:
               INT choiceCount = listView.getCount();
               SparseBooleanArray spBoolArray = listView.getCheckedItemPositions();               DBAufgaben分贝=新DBAufgaben(MainActivity.getMContext());
               db.open();               的for(int i = 0; I< choiceCount;我++){
                   如果(spBoolArray.get(ⅰ)){
                       db.deletContact(listView.getItemIdAtPosition(I));
                   }               }
                光标光标= db.getAllRecords();
                AdapterEingang adapterE =新AdapterEingang(MainActivity.getMContext(),光标,0);
                listView.setAdapter(adapterE);
               db.close();
               mode.finish();
               打破;
           案例R.id.actionmode_cancel:
               mode.finish();
               打破;
           }
           返回false;
       }
    };  //......//  @覆盖
  公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){      查看rootView = NULL;
      。INT位置= getArguments()调用getInt(KE​​Y_POSITION,-1);      开关(位置){
      情况下0:
          rootView = inflater.inflate(R.layout.pager_list,NULL);
          ListView控件=(ListView控件)rootView.findViewById(R.id.pager_list);          上下文的背景下= MainActivity.getMContext();          DBAufgaben DB =新DBAufgaben(背景);          db.open();
          光标光标= db.getAllRecords();
          AdapterEingang adapterE =新AdapterEingang(上下文,光标,0);
          listView.setAdapter(adapterE);
          db.close();          listView.setOnItemLongClickListener(新OnItemLongClickListener(){                @覆盖
                公共布尔onItemLongClick(适配器视图<>适配器视图,视图来看,
                        INT位置,长的id){
                    如果(!isMultipleList){
                        acMode = MainActivity.getInstance()startActionMode(modeCallBack)。
                        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                        listView.setItemChecked(位置,真正的);
                        isMultipleList = TRUE;
                        counterChecked ++;
                        setNewTitle();
                    }其他{
                        listView.setItemChecked(位置,真正的);
                        counterChecked ++;
                        setNewTitle();
                    }                    返回true;
                }              });
          listView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>适配器视图,视图观点,INT位置,
                    长ID){
                Log.d(getTag(),Datensatz:+将String.valueOf(ID));
                如果(isMultipleList){
                    如果(listView.isItemChecked(位置)){
                        listView.setItemChecked(位置,真正的);
                        counterChecked ++;
                        setNewTitle();
                    }其他{
                        listView.setItemChecked(位置,FALSE);
                        counterChecked--;
                        setNewTitle();
                    }                }            }          });
          打破;
      默认:
          rootView = inflater.inflate(R.layout.frag_dummy,NULL);
          TextView的TXT =(TextView中)rootView.findViewById(R.id.dummy_txt);
          txt.setText(将String.valueOf(位置));
          打破;
      }      回报(rootView);
  }
  公共无效setNewTitle(){
      如果(counterChecked == 1){
            acMode.setTitle(counterChecked +Aufgabe);
        }其他{
            acMode.setTitle(counterChecked +Aufgaben);
        }
  }
  @覆盖
  公共无效的onPause(){
      super.onPause();
      如果(isMultipleList){
          acMode.finish();
      }
  }
}


解决方案

ViewPagers保持活跃在任一时间多个页面(默认情况下,页面之前和页面当前显示的页面之后),因此为什么在onPause()不叫直到你刷两页了。

您最好的选择将是使用<一个href=\"https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html\"相对=nofollow> ViewPager.OnPageChangeListener 以及显示和隐藏在使用onPageSelected(..)(即如果所选择的页面不是一个具有ActionMode,隐藏ActionMode)。你可能会在哪些主机您ViewPager活动来实现这一点。

i implemented a contextual action mode bar in a nested fragement. This fragment is part of a view pager and the view pager is also a fragment and part of a navigation drawer.

My Problem: I want to close the contextual action mode bar if the fragment is no more focused. So, if I swipe through the view pager the action mode bar should close. But if I use the onPause() method of the nested fragment, the method is not called directly. Often it waits until i swiped two or three times forward... Here are some pictures:

In the second picture you can see that the action mode bar is still there. So my question is: In which method should I call my actionModeBar.finish() method, to close directly the action mode bar if i leave the fragment?

Maybe the code of the fragment helps you:

public class EditorFragment extends Fragment {

  private static final String KEY_POSITION="position";
  ListView listView;
  private boolean isMultipleList = false;
  private ActionMode acMode;
  private int counterChecked = 0;

  private ActionMode.Callback modeCallBack = new ActionMode.Callback() {

    public boolean onPrepareActionMode(ActionMode mode, Menu menu){
           return false;
       }

      public void onDestroyActionMode(ActionMode mode) {
          listView.clearChoices();
            for (int i = 0; i < listView.getChildCount(); i++)
                listView.setItemChecked(i, false);
                listView.post(new Runnable() {
                    @Override
                    public void run() {
                        listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
                    }
                });
          isMultipleList = false;
          counterChecked = 0;
          mode = null;
       }

       public boolean onCreateActionMode(ActionMode mode, Menu menu) {
           mode.setTitle("1 Aufgabe");
           mode.getMenuInflater().inflate(R.menu.actionmode, menu);
           return true;
       }

       public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
           switch (item.getItemId()) {
           case R.id.actionmode_delete:
               int choiceCount = listView.getCount();
               SparseBooleanArray spBoolArray = listView.getCheckedItemPositions();

               DBAufgaben db = new DBAufgaben(MainActivity.getMContext());
               db.open();

               for (int i = 0; i < choiceCount; i++) {
                   if(spBoolArray.get(i)){
                       db.deletContact(listView.getItemIdAtPosition(i));
                   }

               }
                Cursor cursor = db.getAllRecords();
                AdapterEingang adapterE = new AdapterEingang(MainActivity.getMContext(), cursor, 0);
                listView.setAdapter(adapterE);
               db.close();
               mode.finish();
               break;
           case R.id.actionmode_cancel:
               mode.finish();
               break;
           }
           return false;
       }
    };

  //......//

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View rootView = null;
      int position = getArguments().getInt(KEY_POSITION, -1);

      switch(position){
      case 0:
          rootView = inflater.inflate(R.layout.pager_list, null);
          listView = (ListView) rootView.findViewById(R.id.pager_list);

          Context context = MainActivity.getMContext();

          DBAufgaben db = new DBAufgaben(context);

          db.open();
          Cursor cursor = db.getAllRecords();
          AdapterEingang adapterE = new AdapterEingang(context, cursor, 0);
          listView.setAdapter(adapterE);
          db.close();

          listView.setOnItemLongClickListener(new OnItemLongClickListener(){



                @Override
                public boolean onItemLongClick(AdapterView<?> adapterView, View view,
                        int position, long id) {
                    if(!isMultipleList){
                        acMode = MainActivity.getInstance().startActionMode(modeCallBack);
                        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                        listView.setItemChecked(position, true);
                        isMultipleList = true;
                        counterChecked++;
                        setNewTitle();                      
                    } else {
                        listView.setItemChecked(position, true);
                        counterChecked++;
                        setNewTitle();
                    }

                    return true;
                }

              });
          listView.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                    long id) {
                Log.d(getTag(), "Datensatz: "+String.valueOf(id));
                if(isMultipleList){
                    if(listView.isItemChecked(position)){
                        listView.setItemChecked(position, true);
                        counterChecked++;
                        setNewTitle();
                    } else {
                        listView.setItemChecked(position, false);
                        counterChecked--;
                        setNewTitle();
                    }

                }

            }

          });
          break;
      default:
          rootView = inflater.inflate(R.layout.frag_dummy, null);
          TextView txt = (TextView) rootView.findViewById(R.id.dummy_txt);
          txt.setText(String.valueOf(position));
          break;
      }



      return(rootView);
  }
  public void setNewTitle(){
      if(counterChecked == 1){
            acMode.setTitle(counterChecked+" Aufgabe");
        } else {
            acMode.setTitle(counterChecked+" Aufgaben");
        }
  }
  @Override
  public void onPause(){
      super.onPause();
      if(isMultipleList){
          acMode.finish();
      }
  }
}

解决方案

ViewPagers keep multiple pages active at any one time (by default, the page before and page after the currently shown page), hence why onPause() is not called until you swipe two pages away.

Your best bet would be to use a ViewPager.OnPageChangeListener, and show and hide the ActionMode in onPageSelected(..) (i.e. if the page selected isn't the one with the ActionMode, hide the ActionMode). You'll likely have to implement this in the Activity which hosts your ViewPager.

这篇关于在片段上下文动作方式 - 如果接近不集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆