我在点击Material Drawer中的项目时尝试切换到另一个活动。我在这个应用程序中使用Material Drawer(com.mikepenz.materialdrawer:library:2.9.8)。问题是当我尝试通过点击项目切换到另一个活动时,它总是驱动到默认情况(切换案例)。
这是我的代码: 的 MainFeed.java
package com.example.anggit.myacademic.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import com.example.anggit.myacademic.R;
import com.example.anggit.myacademic.Tab.MyAdapter;
import com.example.anggit.myacademic.Tab.SlidingTabLayout;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.accountswitcher.AccountHeader;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
public class MainFeed extends AppCompatActivity {
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
//Drawer
private Drawer.Result navigationDrawerLeft;
private AccountHeader.Result headerNavigationLeft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager=(ViewPager)findViewById(R.id.vp_tabs);
mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), this));
mSlidingTabLayout=(SlidingTabLayout)findViewById(R.id.stl_tabs);
mSlidingTabLayout.setDistributeEvenly(true);
mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mSlidingTabLayout.setCustomTabView(R.layout.tab_view, R.id.tv_tab);
mSlidingTabLayout.setViewPager(mViewPager);
//=========================================================================================
headerNavigationLeft = new AccountHeader().withActivity(this).withCompactStyle(false).withSavedInstance(savedInstanceState).withHeaderBackground(R.drawable.userprofilebg).addProfiles(
new ProfileDrawerItem().withName("Anggit Prayogo").withEmail("anggitprayogo@gmail.com").withIcon(getResources().getDrawable(R.drawable.nopicture))
).build();
navigationDrawerLeft = new Drawer()
.withActivity(this)
.withToolbar(toolbar)
.withDisplayBelowToolbar(false)
.withActionBarDrawerToggleAnimated(true)
.withDrawerGravity(Gravity.LEFT)
.withSavedInstance(savedInstanceState)
.withAccountHeader(headerNavigationLeft)
.withSelectedItem(0)
.build();
navigationDrawerLeft.addItem(new PrimaryDrawerItem().withName("Profil").withIcon(getResources().getDrawable(R.drawable.ic_face_black_24dp)));
navigationDrawerLeft.addItem(new PrimaryDrawerItem().withName("Setting").withIcon(getResources().getDrawable(R.drawable.ic_settings_24dp)));
navigationDrawerLeft.addItem(new PrimaryDrawerItem().withName("Logout").withIcon(getResources().getDrawable(R.drawable.logout)));
navigationDrawerLeft.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
Intent intent = null;
switch (drawerItem.getIdentifier()) {
case 1:
intent = new Intent(MainFeed.this, EditProfil.class);
startActivity(intent);
break;
default:
Log.d("Test","Default");
break;
}
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
感谢您的帮助:)。
答案 0 :(得分:0)
试试这个
navigationDrawerLeft.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
Intent intent = null;
switch (position) {
case 1:
intent = new Intent(MainFeed.this, EditProfil.class);
startActivity(intent);
break;
default:
Log.d("Test","Default");
break;
}
}
});
答案 1 :(得分:-1)
列表的索引始终以0
开头 navigationDrawerLeft.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
Intent intent = null;
switch (drawerItem.getIdentifier()) {
case 0:
intent = new Intent(MainFeed.this, EditProfil.class);
startActivity(intent);
break;
default:
Log.d("Test","Default");
break;
}
}
});
在您的开关案例中使用0而不是1