【问题标题】:Add filter in toolbar in fragment [duplicate]在片段的工具栏中添加过滤器[重复]
【发布时间】:2019-02-22 07:14:25
【问题描述】:

我想在工具栏中添加过滤器选项,它可以在多个片段中使用,每个片段都有不同的过滤器。例如,如果我单击 A 片段,则会出现一个带有今天日期的过滤器选项的工具栏,如果我单击 B 片段,则会出现一个带有此开始月份日期的过滤器选项的工具栏。 我在每个片段中都得到了图标,但是如何在片段中使用点击事件

  private void setupToolBar() {
    drawerLayout = findViewById(R.id.drawer_layout);
    initNavigationDrawer();
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_menu, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_filter:
            Toast.makeText(this,"Main activity",Toast.LENGTH_LONG).show();
            break;
        default:
            break;
    }
    return true;
}

【问题讨论】:

标签: android


【解决方案1】:

这样试试

//这里我通过使用字符串你可以检查当前的Fragment,所以它会根据Fragment改变

public class YourActivity extends Activity {
  private Menu menu;
  private String DateTitle = "Date'";
  private String MonthTitle = "Month";
  private boolean inBed = false;
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    // Create your menu...
    this.menu = menu;
    return true;
  }
  private void updateMenuTitles() {
    MenuItem bedMenuItem = menu.findItem(R.id.action_filter);

    >//Here i am doing by using string you can check current Fragment so it will change according to Fragment 

if (inBed) {
  bedMenuItem.setTitle(MonthTitle);
} else {
  bedMenuItem.setTitle(DateTitle);
}
  }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.action_filter:
        if(item.getTitle().toString() == "Date"){
        Toast.makeText(this,"Date",Toast.LENGTH_LONG).show();
        }            
        if(item.getTitle().toString() == "Month")
        Toast.makeText(this,"Month",Toast.LENGTH_LONG).show();
        break;
    default:
        break;
}
return true;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多