Почему моя панель инструментов не устанавливает меню на фрагмент? - PullRequest
0 голосов
/ 06 декабря 2018

Я создаю приложение для покупок Android, в котором есть пункт меню корзины, и все продукты открываются фрагментарно, и я хочу установить пункты меню MainActivity для фрагмента продукта и других фрагментов.

Таким образом, пользователю легкооткрыть корзину из вида товара фрагмент. ниже код

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.product_view_fragment, null);
    setHasOptionsMenu(true);
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.setSupportActionBar(toolbar);
    getActivity().invalidateOptionsMenu();
    toolbar = (Toolbar)view.findViewById(R.id.toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().onBackPressed();
        }
    });
    this.context = getContext();
    String id=getArguments().getString("message");
    //String size=getArguments().getString("size");
    viewPager = (ViewPager)view.findViewById(R.id.viewPager);
    SessionManager sessionManager = new SessionManager(getContext());
     email = sessionManager.getUserDetails().get("id");
    sliderDotspanel = (LinearLayout)view.findViewById(R.id.SliderDots);

    GetOneProduct(id);
    return view;
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:segmentedgroup="http://schemas.android.com/tools"
android:orientation="vertical"
android:clickable="true"
android:background="@color/colorWhite"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@android:style/Animation.Activity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:navigationIcon="?attr/homeAsUpIndicator"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:titleTextColor="@color/Black" />
</android.support.design.widget.AppBarLayout>


Blockquote

1 Ответ

0 голосов
/ 06 декабря 2018

Я предполагаю, что вы используете подход с одним действием.Тогда ваша деятельность должна включать панель инструментов в макет примерно так.

 <android.support.constraint.ConstraintLayout 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=".navigation.HomeActivity">
    <androidx.appcompat.widget.Toolbar></androidx.appcompat.widget.Toolbar>
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/app_navigation" />
   <com.google.android.material.bottomnavigation.BottomNavigationView> 
   // if you have bottom nav.
   </com.google.android.material.bottomnavigation.BottomNavigationView>

Затем настройте панель инструментов так, как вы хотите в своей деятельности.

...