我如何使用菜单项选择的选项附加片段

How do i attach fragments using on options item selected using menu items

您好,我有一个包含菜单项的菜单布局。菜单项有一个带有动作视图 class 的项目,它应该在点击时附加一个片段。使用菜单项 class 的操作视图 class 单击 action_account 菜单项时,如何附加 AccountManagementFragment

我的菜单项布局


<item android:id="@+id/action_account"
        android:title="Account"
        android:icon="@drawable/ic_profile"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="ke.co.clinton.hama.viewslogic.account.AccountManagementFragment"/>

我的 onCreateOptionsMenu 和 onOptionsItemsSelected 方法


@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (item.getItemId() == R.id.search) {

        }

        return super.onOptionsItemSelected(item);

    }

我的片段class


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import ke.co.clinton.hama.MainActivity;
import ke.co.clinton.hama.R;
import ke.co.clinton.hama.interfaces.DrawerLocker;

public class AccountManagementFragment extends Fragment {

    private AccountManagementViewModel mViewModel;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ((MainActivity)getActivity()).getSupportActionBar().hide();
        ((DrawerLocker) getActivity()).setDrawerEnabled(false);
        View view = inflater.inflate(R.layout.fragment_account_management, container, false);
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.setTitle("Title");
        toolbar.setNavigationIcon(R.drawable.ic_back_button);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(AccountManagementViewModel.class);
        // TODO: Use the ViewModel

    }


    @Override
    public void onResume() {
        super.onResume();
        ((MainActivity)getActivity()).getSupportActionBar().hide();
        ((DrawerLocker) getActivity()).setDrawerEnabled(true);
    }
    @Override
    public void onStop() {
        super.onStop();
        ((MainActivity)getActivity()).getSupportActionBar().show();
        ((DrawerLocker) getActivity()).setDrawerEnabled(true);
    }

}

您不能在菜单项中将 Fragment 作为 app:actionViewClass 传递。您真正可以做的是在 onOptionItemSelected.

中选择菜单项时附加片段
  1. 从菜单项中删除 app:actionViewClass

    <item android:id="@+id/action_account"
        android:title="Account"
        android:icon="@drawable/ic_profile"
        app:showAsAction="collapseActionView|ifRoom"/>
    
  2. AccountManagementFragment里面添加静态方法获取实例

    public class AccountManagementFragment extends Fragment {
        ...
        ...
    
        static AccountManagementFragment newInstance() {
            return AccountManagementFragment();
        }
        ...
        ...
    }
    
  3. 更改onOptionsItemSelected的实现以添加片段

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    
        if (item.getItemId() == R.id.action_account) {
            // Make sure to add a fragment container view in the layout (preferably a FrameLayout)
            // Here I am assuming the id is 'container'
            getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, AccountManagementFragment.newInstance())
                .commit()
        }
    
        return super.onOptionsItemSelected(item);
    }