单击片段上的溢出菜单时打开底部 sheet

Opening bottom sheet when clicked on the overflow menu on a fragment

我正在尝试实现在点击溢出菜单时打开底部 sheet 的行为。例如:expected behavior

我可以按照建议 here 使用 onMenuOpened 在 activity 上执行此操作, 但我想在片段上做这个。

如何在片段上实现这种行为?

我正在使用单个 activity 模式和导航架构组件。

 **You can try the following steps to open bottom sheet dialog:** 

1. Just make a function inside Activity where the fragment is replace


public Fragment getCurrentFragment() {
      return getSupportFragmentManager().findFragmentById(R.id.frameContainer);
     }

     Fragment fragment = getCurrentFragment();
     if (fragment != null) {
      if (fragment instanceof RequiredFragment) {
       RequiredFragment.openBottumSheetDialog();

      }
     }



2. In Side RequiredFragment get your function from activity:



 private BottomSheetDialog mBottomSheetDialogFragment;

 private void showBottomSheetFilter() {
   if (mBottomSheetDialogFragment == null) {
    mBottomSheetDialogFragment = mBottomSheetDialogFragment.newInstance(feedSection);
    mBottomSheetDialogFragment.setCallBackListener(new OnFeedsTypeSelectedListener() {
      @Override
      public void onFeedsTypeSelected(int contentType) {
       filterByContentTypeId(contentType);
      }

     }
     mBottomSheetDialogFragment.show(getChildFragmentManager(),
      mBottomSheetDialogFragment.getTag());
    }

3. Create a BottomSheetDialog Dialog fragment.



public class BottomSheetDialog extends BottomSheetDialogFragment {

     private String[] feedsFilter;
     private ListView listView;


     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      feedsFilter = getResources().getStringArray(R.array.ideas_filter);
     }

     @Override
     public void setupDialog(final Dialog dialog, int style) {
      super.setupDialog(dialog, style);
      View contentView = View.inflate(getContext(), R.layout.dialog_idea_filter_bottom_sheet, null);
      dialog.setContentView(contentView);

      listView = (ListView) contentView.findViewById(R.id.listView);
      ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(),
       android.R.layout.simple_list_item_1, feedsFilter);
      listView.setAdapter(adapter);

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView < ? > parent, View view,
        int position, long id) {
        if (onFeedsTypeSelected != null) {
         onIdeaTypeSelectedListenonFeedsTypeSelecteder.onFeedsTypeSelected(feedsFilter[position]);
        }
        dismiss();
       }
      });
     }


     public void setCallBackListener(onFeedsTypeSelected SelectedListener onFeedsTypeSelected) {
      this.onIdeaTypeSelectedLionFeedsTypeSelectedstener = onFeedsTypeSelected;
     }
    }

创建一个将由您的 Fragment 实现的接口

例如:

public interface OnMenuOpenListener(){
  boolean onMenuOpened(); 
}

public class MyFragment extends Fragment implements OnMenuOpenListener{
   @Override
   public boolean onMenuOpened(){
    //open bottom sheet here
   }
}

public class MyActivity extends Activity{
   @Override
   public boolean onMenuOpened(int featureId, Menu menu) {
      if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR && menu != null){
        //overflow menu clicked, put code here...
        // As you are using navigation component
        Fragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host);
        //MyFragment
        Fragment fragment=navHostFragment.getChildFragmentManager().getFragments().get(0);
        if(fragment instanceof OnMenuOpenListener){
           ((OnMenuOpenListener)fragment).onMenuOpened()
          return false;
        }
      }
       return super.onMenuOpened(featureId, menu);
   }
}

由于支持操作栏已附加到 Activity 所有事件都由 Activity 捕获,您需要做的就是获取需要事件的片段并使用调用 back.If 你 return false onMenuOpened 将不会打开溢出菜单,并且会触发你片段中的底部 sheet 菜单。

P.S- 我没有在编辑器中编写代码,所以可能有一些错误,但你一定明白了。

参考:

正如所讨论的那样 here 当点击溢出菜单时打开底部 sheet 是糟糕的用户体验。

为什么?

引用自post

Because user have to reach the top of the screen to click the oveflow menu, then go back to the bottom to click desired action which is on the bottom sheet.

-

According to Fitt's Law - The time to acquire a target is a function of the distance to and size of the target. I agree that I think distance between the menu and the bottom sheet is substantial. This solution allows placing a lot options in one place.

-

it also doesn't match the user expectation since people are used to the overflow menu opening in a different manner.

如果您有顶部操作栏,请使用通常的上下文菜单。如果你有底部操作栏,你可以使用 bottom sheet.