在片段

时间:2016-09-04 12:13:47

标签: android android-fragments fragment

@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();

    //noinspection 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);
}

我在片段中添加了,但我没有' t,如果我添加MainActivity,它可以工作,但我想在Fragment中调用。我怎么能这样做?

4 个答案:

答案 0 :(得分:43)

片段中,您必须致电setHasOptionsMenu(true)

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

然后假设你必须处理menu_item_to_handle_in_fragment项目点击

  

For Fragment class

  @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;
    }

答案 1 :(得分:5)

您需要在片段的onCreate中添加setHasOptionMenu(true) 添加此选项时,片段生命周期将调用onCreateOptionMenu()和onOptionItemSelected()。

请按照以下步骤操作:

  • 在片段的onCreate()中添加setHasOptionsMenu(true)方法。

  • 覆盖onCreateOptionsMenu(菜单菜单,MenuInflater inflater)和 片段中的onOptionsItemSelected(MenuItem item)方法。

  • 在onOptionsItemSelected(MenuItem item)Activity的方法中, 确保在菜单项操作时返回false 在onOptionsItemSelected(MenuItem item)Fragment中实现 方法

答案 2 :(得分:0)

在片段

中创建选项菜单的步骤

1

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

2。

@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;
    }
}

答案 3 :(得分:0)

好的,有很多答案,但以上都没有显示如何实际调用 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)
    }
}