gpt4 book ai didi

android - 单击工具栏上的汉堡包图标不会打开抽屉导航

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:57 26 4
gpt4 key购买 nike

我有这个抽屉导航,它工作得很好。重构我的代码,我删除了 Activity 中的所有 onOptionsItemSelecteds 并使所有 Activity 继承自扩展 AppComplatActivity实现所有必要方法的基本 Activity 。在点击汉堡包图标后,即使我有 syncstate() 和所有东西,它也不再起作用。

为什么这不起作用的任何线索?

One of the activities:

public class MainActivity extends BaseActivity implements SearchFilterFragment.OnFragmentInteractionListener {

NavigationView navigationView;
DrawerLayout drawerLayout;

private Tracker mTracker;

@Override
protected void onResume() {
super.onResume();
drawerLayout.openDrawer(GravityCompat.START);
}

@Override
protected void onPostResume() {
super.onPostResume();
mTracker.setScreenName("MainActivity" + "-----");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.openDrawer(GravityCompat.START);
navigationView = (NavigationView) findViewById(R.id.navigation_view_primary);
navigationView.setNavigationItemSelectedListener(new NavigationDrawerListener(this));
setupToolbar();
Haftdong application = (Haftdong) getApplication();
mTracker = application.getDefaultTracker();
}

private void setupToolbar() {
// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();//for hamburger icon
}

@Override
public void onFragmentInteraction(Uri uri) {
}

基础 Activity :

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

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

@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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

最佳答案

您正在为 ActionBarDrawerToggle 使用四参数构造函数,这意味着您必须调用开关的 onOptionsItemSelected() MainActivity 中的方法的 onOptionsItemSelected()覆盖以打开/关闭抽屉。

例如:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}

如果您恰好提供自己的 Toolbar – 例如,作为支持ActionBar (虽然没有必要这样设置)——然后你可以传递 Toolbar作为 ActionBarDrawerToggle 中的第三个参数构造函数调用。例如:

Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

抽屉打开/关闭将由 ActionBarDrawerToggle 处理在内部,您不需要调用 onOptionsItemSelected() 中的切换开关.

setDisplayHomeAsUpEnabled()此设置也不需要调用,如果您不想设置 Toolbar,这很方便作为ActionBar .

关于android - 单击工具栏上的汉堡包图标不会打开抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36570450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com