【问题标题】:Action Bar Items are clickable, but do not respond to events in a Fragment操作栏项目是可点击的,但不响应片段中的事件
【发布时间】:2019-01-13 15:31:31
【问题描述】:

基本上我的项目必须有不同的工具栏,一个用于底部导航栏切换的每个片段。 片段工具栏项目是可点击的,当我长按时,显示标题项目等,但它们不执行操作。例如,当我设置在单击该项目时显示敬酒时,他将不会执行。有什么建议吗?谢谢。

public class FeedFragment extends Fragment {

    public static final String TAG = "Tag Free";

    DialogFragment mDialog;
    FloatingActionButton fab;
    RevealFrameLayout reveal;
    FrameLayout frame;
    private ConstraintLayout layoutDialog;
    boolean isOpen = false;

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_feed, container, false);

        /** Toolbar **/
        Toolbar myToolbar = (Toolbar) view.findViewById(R.id.my_toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar);

        ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(false);

        // navigation bottom
        fab = getActivity().findViewById(R.id.fab_button);
        fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_pencil_black_24dp));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            FragmentTransaction ft = getFragmentManager().beginTransaction().setCustomAnimations(R.anim.hold, R.anim.slide_down);
            Fragment prev = getFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);
            DialogFragment dialogFragment = new PostDialog();
            dialogFragment.show(ft, "dialog");
            dialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme);

            }
        });

        return view;
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_personProfile:
            Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getActivity(), MyUserProfileActivity.class);
            startActivity(intent);
            return true;

            case R.id.action_settings:
            Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
            return true;


            default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);
        }
    }
}

【问题讨论】:

    标签: android android-fragments android-actionbar android-toolbar


    【解决方案1】:

    您没有在活动方法中链接到超类。

    重构你的代码如下:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.appbar_feed, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_personProfile:
                Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), MyUserProfileActivity.class);
                startActivity(intent);
                break;
    
            case R.id.action_settings:
                Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
                break; 
    
    
            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                break; 
    
        }
      return super.onOptionsItemSelected(item);
    }
    

    希望您的代码现在可以正常工作。

    【讨论】:

    • 仍然不能使用编辑。也许是我的 Activity 上有什么东西,不是吗?
    • 你能在日志中记录一些看是否触发。让我知道。
    • 您能否为您的特定开关案例调试 item.getItemId()。
    • 我搞定了,看日志prnt.sc/kft95b是Always The Default Option。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多