是否可以使用 bottomNavigationBar 打开 bottomSheetDialog?
Is it possible to open bottomSheetDialog with bottomNavigationBar?
我想在单击底部导航项时打开底部 sheet 对话框(类似 Yandex Maps android 应用程序。背景上始终有地图。底部导航项只是打开底部 sheet对话框)。
但是我不能那样做。这可能吗?如果可能的话?
我尝试了一些片段方法,但应用程序崩溃了。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/berkay"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.mapbox.mapboxsdk.maps.MapView>
<FrameLayout
android:id="@+id/frame_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/main_bottom">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/navigation"
android:background="?android:attr/windowBackground">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
底部导航项选择
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_map:
break;
case R.id.navigation_search:
SearchFragment searchFragment = new SearchFragment();
searchFragment.show(getSupportFragmentManager(),"SearchFragment");
break;
case R.id.navigation_event:
selectedFragment = new EventsFragment();
break;
case R.id.navigation_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.frame_main,selectedFragment).commit();
return true;
}
};
SearchFragment.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapboxMap;
public class SearchFragment extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search,container,false);
return v;
}
}
Logcat SearchFragment 案例错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
我做到了
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_map:
MapFragment mapFragment = new MapFragment();
mapFragment.show(getSupportFragmentManager(),"MapFragment");
break;
case R.id.navigation_search:
SearchFragment searchFragment = new SearchFragment();
searchFragment.show(getSupportFragmentManager(),"SearchFragment");
break;
}
return true;
}
};
我想在单击底部导航项时打开底部 sheet 对话框(类似 Yandex Maps android 应用程序。背景上始终有地图。底部导航项只是打开底部 sheet对话框)。
我尝试了一些片段方法,但应用程序崩溃了。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/berkay"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.mapbox.mapboxsdk.maps.MapView>
<FrameLayout
android:id="@+id/frame_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/main_bottom">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/navigation"
android:background="?android:attr/windowBackground">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
底部导航项选择
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_map:
break;
case R.id.navigation_search:
SearchFragment searchFragment = new SearchFragment();
searchFragment.show(getSupportFragmentManager(),"SearchFragment");
break;
case R.id.navigation_event:
selectedFragment = new EventsFragment();
break;
case R.id.navigation_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.frame_main,selectedFragment).commit();
return true;
}
};
SearchFragment.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapboxMap;
public class SearchFragment extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search,container,false);
return v;
}
}
Logcat SearchFragment 案例错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
我做到了
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_map:
MapFragment mapFragment = new MapFragment();
mapFragment.show(getSupportFragmentManager(),"MapFragment");
break;
case R.id.navigation_search:
SearchFragment searchFragment = new SearchFragment();
searchFragment.show(getSupportFragmentManager(),"SearchFragment");
break;
}
return true;
}
};