单击项目 Material 抽屉时更改为另一个 activity
Change to another activity when item Material Drawer is clicked
我正在尝试在单击 Material 抽屉中的项目时切换到另一个 activity。我在此应用程序中使用 Material 抽屉 (com.mikepenz.materialdrawer:library:2.9.8)。问题是当我尝试通过单击项目切换到另一个 activity 时,它总是驱动到默认大小写(切换大小写)。
这是我的代码:
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);
}
}
感谢帮助:)
试试这个
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;
}
}
});
列表的索引始终以 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;
}
}
});
在你的 switch case 中使用 0 而不是 1
我正在尝试在单击 Material 抽屉中的项目时切换到另一个 activity。我在此应用程序中使用 Material 抽屉 (com.mikepenz.materialdrawer:library:2.9.8)。问题是当我尝试通过单击项目切换到另一个 activity 时,它总是驱动到默认大小写(切换大小写)。
这是我的代码: 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);
}
}
感谢帮助:)
试试这个
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;
}
}
});
列表的索引始终以 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;
}
}
});
在你的 switch case 中使用 0 而不是 1