OnDrawerItemClickListener 不起作用。
OnDrawerItemClickListener Does not work.
我一直在使用 toolbar.Now 开发这个导航抽屉,我一直在使用点击监听器。当我点击抽屉里的物品时,它什么也没做。我的 class 是片段。这是代码。
包裹 com.vivek.ss;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
public class MainActivity extends ActionBarActivity {
private BaseFragment mBaseFragment;
private int mSelectedFragment;
private static String BUNDLE_SELECTEDFRAGMENT = "Selected";
private static final int CASE_START = 0;
private static final int CASE_VIEW = 1;
private Drawer.Result result = null;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(BUNDLE_SELECTEDFRAGMENT , mSelectedFragment);
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar;
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState != null) {
mSelectedFragment = savedInstanceState.getInt(BUNDLE_SELECTEDFRAGMENT);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (fragmentManager.findFragmentById(R.id.frame_container) == null)
mBaseFragment = selectFragment(mSelectedFragment);
}
result = new Drawer().withActivity(this).withToolbar(toolbar).withActionBarDrawerToggle(true).withHeader(R.layout.header)
.addDrawerItems(new PrimaryDrawerItem().withName(R.string.primaryfirst)
, new PrimaryDrawerItem().withName(R.string.primarysecond),
new SecondaryDrawerItem().withName(R.string.secondaryfirst)
).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id, IDrawerItem iDrawerItem) {
if (iDrawerItem != null) {
if (iDrawerItem instanceof PrimaryDrawerItem) {
toolbar.setTitle(((PrimaryDrawerItem) iDrawerItem).getNameRes());
mBaseFragment = selectFragment(position);
mSelectedFragment = position;
}
}
}
}).build();
}
private BaseFragment selectFragment(int position) {
BaseFragment baseFragment = null;
switch (position) {
case CASE_START:
baseFragment = new TestFragment();
break;
case CASE_VIEW:
baseFragment = new TestFragment();
break;
}
return baseFragment;
}
@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_main, 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);
}
}
clicking on the items it changes the action bar title correspondingly.
But my fragment wont start
要在onItemClick 上显示Fragment 需要使用FragmentTransaction 的replace
或add
方法。从 onItemClick
方法开始片段:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, mBaseFragment);
fragmentTransaction.addToBackStack(null);
// Commit the transaction
fragmentTransaction.commit();
我一直在使用 toolbar.Now 开发这个导航抽屉,我一直在使用点击监听器。当我点击抽屉里的物品时,它什么也没做。我的 class 是片段。这是代码。 包裹 com.vivek.ss;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
public class MainActivity extends ActionBarActivity {
private BaseFragment mBaseFragment;
private int mSelectedFragment;
private static String BUNDLE_SELECTEDFRAGMENT = "Selected";
private static final int CASE_START = 0;
private static final int CASE_VIEW = 1;
private Drawer.Result result = null;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(BUNDLE_SELECTEDFRAGMENT , mSelectedFragment);
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar;
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState != null) {
mSelectedFragment = savedInstanceState.getInt(BUNDLE_SELECTEDFRAGMENT);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (fragmentManager.findFragmentById(R.id.frame_container) == null)
mBaseFragment = selectFragment(mSelectedFragment);
}
result = new Drawer().withActivity(this).withToolbar(toolbar).withActionBarDrawerToggle(true).withHeader(R.layout.header)
.addDrawerItems(new PrimaryDrawerItem().withName(R.string.primaryfirst)
, new PrimaryDrawerItem().withName(R.string.primarysecond),
new SecondaryDrawerItem().withName(R.string.secondaryfirst)
).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id, IDrawerItem iDrawerItem) {
if (iDrawerItem != null) {
if (iDrawerItem instanceof PrimaryDrawerItem) {
toolbar.setTitle(((PrimaryDrawerItem) iDrawerItem).getNameRes());
mBaseFragment = selectFragment(position);
mSelectedFragment = position;
}
}
}
}).build();
}
private BaseFragment selectFragment(int position) {
BaseFragment baseFragment = null;
switch (position) {
case CASE_START:
baseFragment = new TestFragment();
break;
case CASE_VIEW:
baseFragment = new TestFragment();
break;
}
return baseFragment;
}
@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_main, 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);
}
}
clicking on the items it changes the action bar title correspondingly. But my fragment wont start
要在onItemClick 上显示Fragment 需要使用FragmentTransaction 的replace
或add
方法。从 onItemClick
方法开始片段:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, mBaseFragment);
fragmentTransaction.addToBackStack(null);
// Commit the transaction
fragmentTransaction.commit();