更改容器页面onClick菜单项

时间:2017-12-26 10:24:40

标签: android android-layout

如何在点击container项目上更改标签Home

bottom_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:id="@+id/action_item1"
        android:icon="@drawable/ic_format_list_bulleted_black_24dp"
        android:title="Status"
        android:onClick="go_page('Stat')"/>
</menu>

来自tabs.xml

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@color/greyish"
        app:itemIconTint="@color/black"
        app:itemTextColor="@color/black"
        app:menu="@menu/bottom_menu"/>

tabs.java

public void go_page(String fragmentId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentId);
        if (fragment == null) {
            if (fragmentId.equals("Stat")) {
                fragment = StatTab.newInstance();
            }
            if (fragmentId.equals("Other")) {
                fragment = OtherTab.newInstance();
            }
            ft.replace(R.id.container, fragment, fragmentId).commit();
        }
    }

StatTab片段:

public class StatTab extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_stat_tab, container, false);
    }
}
  

无法解析名称newInstance()

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您需要在newInstance()StatTab OtherTab中使用fragment方法,如下面的代码

 public class StatTab extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_stat_tab, container, false);
        }

        public  static StatTab newInstance() {
        return new StatTab();
      }
    }


 public class OtherTab extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_other_tab, container, false);
        }

        public static OtherTab newInstance() {
        return new OtherTab();
      }
    }