【问题标题】:Change container page onClick a menu item单击菜单项更改容器页面
【发布时间】:2017-12-26 10:24:40
【问题描述】:

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

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()

感谢您的帮助

【问题讨论】:

  • 您没有在 StatTab.java 中声明的 static 方法 newInstance()。如果您没有这样的方法,您希望发生什么?

标签: android android-layout


【解决方案1】:

您需要在 StatTab 和 OtherTab fragment 中创建 newInstance() 方法,如下代码所示

 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();
      }
    }

【讨论】:

  • 在 xml 文件 android.view.InflateException: Binary XML file line #53: Error inflating class android.support.design.widget.BottomNavigationView987654327@ 中添加 onClick 出现问题
  • @user3449848 没有得到你
  • 我无法从bottom_menu.xml 为public void go_page(String fragmentId) 实现android:onClick="go_page('Stat')"。还有其他替代方法吗?
  • @user3449848 从 menu.xml 中删除 android:onClick="go_page('Stat')"
  • @user3449848 这将帮助你stackoverflow.com/questions/40202294/…
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 2013-06-13
  • 2016-02-07
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 2020-07-24
  • 2022-08-07
相关资源
最近更新 更多