菜单未显示在自定义工具栏中的片段中

时间:2019-06-23 08:18:05

标签: android fragment

我为自己创建的片段创建了一个自定义工具栏,它应该看起来像这样:

enter image description here

左侧显示的工具栏来自我的fragment_purchase_item_list,右侧显示的是我的fragment_purchase_list

我已经尝试从这里检查其他一些帖子,但是我将不会显示我创建的菜单。

运行代码时,它将如下所示:

enter image description here

搜索图标(左),扫描和新图标(右)将不会显示。这是我的代码:

PurchaseItemListFragment.java

package com.example.devcash.Fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.devcash.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class PurchaseItemListFragment extends Fragment implements SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {

    Toolbar itemListToolbar;
    Spinner itemListSpinner;


    public PurchaseItemListFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //inflate the  menu
        setHasOptionsMenu(true);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_purchase_item_list, container, false);


        itemListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaseitemlist);
        itemListSpinner = (Spinner) view.findViewById(R.id.spinner_allcategories);

        ///
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.custom_spinner_item,
                getResources().getStringArray(R.array.dropdownitempurchase));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        itemListSpinner.setAdapter(myAdapter);

        itemListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(),
                        itemListSpinner.getSelectedItem().toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        return view;
    }

    //handles the search menu
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.searchmenu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("Search..");

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }


    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        return true;
    }
}

PurchaseListFragment.java

package com.example.devcash.Fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.devcash.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class PurchaseListFragment extends Fragment {

    Toolbar purchaseListToolbar;
    Spinner purchaseListSpinner;


    public PurchaseListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_purchase_list, container, false);

        setHasOptionsMenu(true);

        purchaseListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaselist);
        purchaseListSpinner = (Spinner) view.findViewById(R.id.spinner_customertype);

        ///
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.customer_type));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        purchaseListSpinner.setAdapter(myAdapter);

        purchaseListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(),
                        purchaseListSpinner.getSelectedItem().toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //set adapter
        //set click listener

        //show no data found text when listview is empty
        // button click listener
        Button btnpay = view.findViewById(R.id.btn_paypurchasetransaction);
        btnpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment chargeFragment = new ChargeFragment();

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.customersales_content, chargeFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.purchaselist_menu, menu);
    }
}

fragment_purchase_item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.PurchaseItemListFragment"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_purchaseitemlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        style="@style/PrimaryHeaderBar"
        android:elevation="4dp">

        <Spinner
            android:id="@+id/spinner_allcategories"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_vertical">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_cry_face"
                android:layout_gravity="center"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/no_items"
                android:gravity="center"
                android:layout_marginTop="@dimen/text_padding"/>

            <Button
                android:layout_width="150dp"
                android:layout_height="30dp"
                android:text="@string/go_inventory"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/text_padding"
                android:background="@color/colorPrimary"
                android:textColor="@color/whiteBG"
                android:textSize="@dimen/small_title"/>

        </LinearLayout>
    </FrameLayout>
</LinearLayout>

fragment_purchase_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.PurchaseListFragment"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_purchaselist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Light"
        style="@style/HeaderBar"
        android:elevation="4dp">

        <Spinner
            android:id="@+id/spinner_customertype"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </android.support.v7.widget.Toolbar>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="@color/light_gray"
        android:padding="@dimen/text_padding"
        android:gravity="center">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:src="@drawable/ic_delete"
            android:tint="@color/dark_text"
            android:layout_gravity="left"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/item_count_title"
                android:gravity="center"
                android:textSize="@dimen/page_title"
                android:textColor="@color/dark_text"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/opencurly"
                android:paddingLeft="@dimen/text_inputs"
                android:textSize="@dimen/page_title"
                android:textColor="@color/dark_text"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/count"
                android:textSize="@dimen/page_title"
                android:textColor="@color/dark_text"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/closecurly"
                android:textSize="@dimen/page_title"
                android:textColor="@color/dark_text"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7"
        android:orientation="horizontal">

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:orientation="vertical"
        android:gravity="bottom"
        android:padding="@dimen/text_padding">

        <Button
            android:id="@+id/btn_paypurchasetransaction"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@color/colorPrimary"
            android:text="@string/charge"
            android:textColor="@color/whiteBG"/>

    </LinearLayout>

</LinearLayout>

purchaselist_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_scan"
        android:title="@string/scan_label"
        android:icon="@drawable/ic_qr_code_scan"
        app:showAsAction="always"
        android:actionLayout="@layout/custom_purchaselist_layout"/>

    <item
        android:id="@+id/action_new"
        android:title="@string/new_label"
        android:icon="@drawable/ic_add"
        app:showAsAction="always"
        android:actionLayout="@layout/custom_purchaselist_add_layout"/>

</menu>

searchmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:title="@string/search_title"
        android:orderInCategory="2"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always" />
</menu>

0 个答案:

没有答案