Android如何从片段调用导航抽屉

时间:2019-09-21 06:28:08

标签: android-fragments navigation-drawer

在我的片段中,我有带有导航按钮的物料搜索栏(humbugger)。 如何调用片段中具有该humbugger按钮的主要活动中的导航抽屉 不了解如何在DictionaryFragment中处理它

MainActivity:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    LinearLayout content = (LinearLayout) findViewById(R.id.content);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
            drawer,
            R.string.nav_open_drawer,
            R.string.nav_close_drawer){

    };
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

DictionaryFragment:

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

materialSearchBar = (MaterialSearchBar) RootView.findViewById(R.id.search_bar);

...

materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {

        @Override
        public void onButtonClicked(int buttonCode) {
             //***HOW TO HANDLE IT HERE?***
            //switch (buttonCode) {
              //  case MaterialSearchBar.BUTTON_NAVIGATION:
              //      drawer.openDrawer(Gravity.START);
              //      break;}
        }
    });
       //return RootView;
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/searchBarDividerColor"
tools:context="com.hfad.catchat.DictionaryFragment">

<com.mancj.materialsearchbar.MaterialSearchBar
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:mt_hint="Search"
    app:mt_navIconEnabled="true"
    app:mt_speechMode="false" />

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_search"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

</android.support.v7.widget.RecyclerView>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

这是您可以尝试在片段中切换导航抽屉的位置的尝试,通过这种方式,您将必须在活动中编写一个方法来执行片段中您想做的事情,请确保它是一个公共方法:

    ((MainActivity)getContext()).toggleDrawer();

在您的MainActivity中:

    public void toggleDrawer(){
    //do your stuff
    }

另一种方法是回调aka接口(首选接口),将其作为片段构造函数中的参数传递,并在要更改抽屉状态的位置使用它。就像在您的活动中一样:

    Callback callback = new Callback(){
    @Override
    public void onDrawerStateChanged(){
      //do your stuff
    }};
    new DictionaryFragment(callback);

在片段中,您将必须编写一个构造函数以接受该回调并保存在本地变量中:

    public DictionaryFragment() {
    }


    @SuppressLint("ValidFragment")
    public DictionaryFragment(Callback callback) {
    this.callback = callback;
    }

并像这样使用它:

    callback.onDrawerStateChanged();

您还可以通过两种方式将参数传递给MainActivity。