Android 如何从 fragment 中调用 Navigation Drawer
Android how to call Navigation Drawer from fragment
在我的片段中,我有 material 带有导航按钮(骗子)的搜索栏。
如何使用片段中的骗子按钮调用主 activity 中的 Navigation Drawer
不明白如何在 DictionaryFragment
里面处理
主要活动:
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();
}
}
字典片段:
@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>
这是您可以尝试在您的片段中切换导航抽屉的地方,这样您就必须在 activity 中编写一个方法来从您的片段中做任何您想做的事情,请确保这是一个 public 方法:
((MainActivity)getContext()).toggleDrawer();
在您的 MainActivity 中:
public void toggleDrawer(){
//do your stuff
}
其他方式是回调 aka 接口(首选方式),将其作为参数传递到片段的构造函数中,并在您想要更改抽屉状态的地方使用它。就像你的 activity:
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 传递参数。
在我的片段中,我有 material 带有导航按钮(骗子)的搜索栏。 如何使用片段中的骗子按钮调用主 activity 中的 Navigation Drawer 不明白如何在 DictionaryFragment
里面处理主要活动:
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();
}
}
字典片段:
@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>
这是您可以尝试在您的片段中切换导航抽屉的地方,这样您就必须在 activity 中编写一个方法来从您的片段中做任何您想做的事情,请确保这是一个 public 方法:
((MainActivity)getContext()).toggleDrawer();
在您的 MainActivity 中:
public void toggleDrawer(){
//do your stuff
}
其他方式是回调 aka 接口(首选方式),将其作为参数传递到片段的构造函数中,并在您想要更改抽屉状态的地方使用它。就像你的 activity:
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 传递参数。