我点击导航栏中的项目底部导航栏我正在替换片段.我有3个片段A,B,C所以点击b项B片段加载,在B我调用3-4个API.所以,现在如果我转到C然后再次来到B,就会创建一个新的B Fragment实例,并再次调用这些API,如何保存片段实例状态,而不是在更改片段时再次调用API.这是我的代码.
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment currentLoaded = fgMan.findFragmentById(R.id.container_body);
switch (id) {
case R.id.nearby_fragment:
if (!(currentLoaded instanceof SpotFeedMapFragment)) {
removeScroll();
mNearByFragment = fgMan.findFragmentByTag(NEARBY_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(NEARBY_FRAGMENT_TAG) : mNearByFragment;
fgMan.beginTransaction().setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
fgMan.beginTransaction().replace(R.id.container_body, mNearByFragment, NEARBY_FRAGMENT_TAG).commit();
fgMan.executePendingTransactions();
getSupportActionBar().setTitle(getString(R.string.nearby_fragment));
}
break;
case R.id.route_fragment:
if (!(currentLoaded instanceof BusLocationsFragment)) {
if (!inParent) {
mRl.removeView(fixLayout);
p.addRule(RelativeLayout.BELOW, toolbar.getId());
scrollView.setLayoutParams(p);
scrollView.addView(fixLayout);
mRl.addView(scrollView);
inParent = true;
}
//mFragment = new BusLocationsFragment();
mBusLocFragment = fgMan.findFragmentByTag(BUS_LOC_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(BUS_LOC_FRAGMENT_TAG) …Run Code Online (Sandbox Code Playgroud) android navigationbar android-fragments bottomnavigationview
我正在使用新的Android BottomNavigationView但底部导航MenuItem操作视图未显示.
调试后,我发现菜单项不为null,可见性可见.
在检查根视图的高度和宽度之后,即使在对布局layout_width=50dp和layout_height=50dp根元素中的值进行硬编码之后它也会变为0 .
这是我的底部导航菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/nearby_fragment"
android:enabled="true"
android:icon="@drawable/homenearbyfragment"
android:title="@string/bottom_nearby"
app:showAsAction="always"/>
<item
android:id="@+id/route_fragment"
android:title="@string/bottom_homescreen"
android:enabled="true"
app:showAsAction="always"
android:icon="@drawable/home_mycommute" />
<item
android:id="@+id/newsfeed_activity"
android:title="@string/bottom_news"
android:enabled="true"
app:showAsAction="always"
android:icon="@drawable/newsfeed"
app:actionLayout="@layout/bagde_layout"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
我的动作布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:gravity="center">
<ImageView
android:id="@+id/menu_badge_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/newsfeed" />
<TextView
android:id="@+id/menu_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginTop="4dp"
android:background="@drawable/circle_bg"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="8sp"
/>
Run Code Online (Sandbox Code Playgroud)
在我Activity我试图设置:
private void setCountOnNews(Menu menu) {
mReportMenu = menu.findItem(R.id.newsfeed_activity);
FrameLayout count = (FrameLayout) mReportMenu.getActionView();
count.setOnClickListener(new …Run Code Online (Sandbox Code Playgroud)