android-fragments 在片段中添加onOptionsItemSelected调用

frebpwbc  于 2022-11-14  发布在  Android
关注(0)|答案(4)|浏览(141)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //no inspection SimplifiableIfStatement
    if (id == R.id.action_filter) {
        FragmentManager fm = getSupportFragmentManager();
        if (userType.equals("İş Arayan"))
            filterDialogTitle = "İş İlanları Filtre";
        else if (userType.equals("Hizmet Arayan"))
            filterDialogTitle = "Hizmet İlanları Filtre";
        FilterDialogFragment editNameDialogFragment = FilterDialogFragment.newInstance(filterDialogTitle);
        editNameDialogFragment.show(fm, "fragment_edit_name");
        return true;
    }

    return super.onOptionsItemSelected(item);
}

我在Fragment中添加了,但是它没有被调用,如果我在MainActivity中添加了,它可以工作,但是我想在Fragment中调用它。我该怎么做呢?

nvbavucw

nvbavucw1#

Fragment中,您必须调用setHasOptionsMenu(true)

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

然后假设您要处理menu_item_to_handle_in_fragment项,单击
对于片段类

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.menu_item_to_handle_in_fragment:
            // Do onlick on menu action here
            return true;
        }
    return false;
}

对于活动类

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

            case R.id.menu_item_to_handle_in_fragment:
                return false;
            }
        return false;
    }
qvtsj1bj

qvtsj1bj2#

您需要在片段的onCreate中添加setHasOptionMenu(true)
添加此选项时,片段生命周期将调用onCreateOptionMenu()与onOptionItemSelected()。
请执行以下步骤:

  • 在片段的onCreate()中添加setHasOptionsMenu(true)方法。
  • 重写片段中的onCreateOptionsMenu(菜单菜单,MenuInflater inflater)和onOptionsItemSelected(菜单项)方法。
  • 在onOptionsItemSelected(MenuItem项)Activity的方法中,确保在onOptionsItemSelected(MenuItem项)Fragment的方法中实现菜单项操作时返回false。
lndjwyie

lndjwyie3#

在片段中创建选项菜单的步骤
1.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_home, menu);
    super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case android.R.id.home:
            //call function as per your requirement
            return true;
        default:
            return false;
    }
}
kupeojn6

kupeojn64#

好吧,有很多答案,上面没有一个显示如何真正调用Frarment。
这是经过测试并正常工作的整个示例。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //activate menu on the top to show icons or menu
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        //this part we need to show back arrow on top so user could use backstack
        setupActionBarWithNavController(findNavController(R.id.fragmentContainer))
        //setupActionBarWithNavController(findNavController(R.id.fragmentContainer))

    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.fragmentContainer)
        return navController.navigateUp() || super.onSupportNavigateUp()
    }

    //show icons in top menu
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        //inflate our menu our menu situated in res menu menu.xml so R.menu.menu(our menu)
        menuInflater.inflate(R.menu.menu, menu)
        return true
    }

    //if item in menu selected do ......
    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        val navController = findNavController(R.id.fragmentContainer)
        when (item.itemId) {
            **R.id.iconAbout -> navController.navigate(R.id.aboutFragment)**

        }
        return super.onOptionsItemSelected(item)
    }
}

相关问题