单击ImageView时如何打开滑动导航抽屉?

时间:2017-05-04 06:14:37

标签: android navigation navigation-drawer

我试图在我的信息中心Navigation Drawer Activity上实施Activity。在我的应用程序中,我创建了一个主题,其中'工具栏/ ActionBar'已删除,只需在ImageView上为我的汉堡菜单添加custom layout,并将其包含在我的信息中心版面上。我想要的是,当点击ImageView时,将显示滑动抽屉。

我尝试做的是通过com.project.projectname添加Navigation Drawer Activity - >新 - >活动 - > '导航抽屉活动'并自动添加NavigationActivity class , activity_navigation.xml app_bar_navigation.xml, etc

但我想知道如何打开抽屉? 在我Dashboard Activity我添加了这个

 public class DashboardActivity extends AppCompatActivity {

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

   View side_tab = findViewById(R.id.side_tab);
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);

  expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // open drawer here 
        }
    });

此外,在NavigationActivity ActionBarDrawerToggle使用ActionBar时,我不得使用此功能,因为我不会应用NavigationActivity。但我可以使用哪种替代方案?

这是我的public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.navigation, 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); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); Intent goToSreen; if (id == R.id.nav_dashboard) { goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class); startActivity(goToSreen); } else if (id == R.id.nav_others) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }

NavigationActivity navigationActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    View side_tab = findViewById(R.id.side_tab);
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);

 expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            navigationActivity = new NavigationActivity() ;
            navigationActivity.drawer.openDrawer(GravityCompat.START);
        }
    });

感谢您的帮助。

更新

所以在我的Dashboard Activity中添加了以下内容

public class NavigationActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

在我的导航活动中,我添加了一个全局变量

drawer

我已设法在我的仪表板活动中调用java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference,但单击汉堡菜单时,滑动菜单仍然无法显示。该应用程序崩溃并显示recyclerView.setHasFixedSize(true);

3 个答案:

答案 0 :(得分:2)

NavigationActivity

中添加此内容
public class NavigationActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

//Add this line
    public static DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
.......

然后改变这个

expanded_menu.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                // open drawer here 
                NavigationActivity. drawer.openDrawer(GravityCompat.START);
        }
   });

试着希望它会解决你的问题。

答案 1 :(得分:1)

NavigationActivity中添加以下代码:

DrawerLayout mDrawerLayout;    // Declare globally

onCreate()

mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
 expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // open drawer here 
            mDrawerLayout.openDrawer(Gravity.LEFT);
        }
    });

如果抽屉已打开,请将其关闭onBackPressed()

@Override
public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
        mDrawerLayout.closeDrawer(Gravity.LEFT);
        return;
    }
}

希望这有帮助。

答案 2 :(得分:1)

我将NavigationActivity扩展到我的DashboardActivity,如下所示

public class DashboardActivity extends NavigationActivity {

    side_tab = findViewById(R.id.side_tab);
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);


  expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            side_tab.setVisibility(View.INVISIBLE);
            drawer.openDrawer(GravityCompat.START);
        }
    });

然后按照@Hassan Usman和@tahsinRupam所说的回答

之后我在DrawerLayout

中添加了dashboard.xml