我为自己的应用创建了自定义导航抽屉。行为是完美的,除了当我点击一个项目时它立即关闭,没有动画。但是没有选择的开启和关闭工作是完美的
public class MainMenuActivity extends ActivityBase implements NavigationInteractions {
private Toolbar mToolbar;
private TextView mToolbarTitle;
private ImageView mToolbarIcon;
private DrawerLayout drawer;
private ActionBarDrawerToggle drawerToggle;
private CustomDrawerAdapter drawerAdapter;
private List<DrawerItem> drawerItemList;
private ListView drawerListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
// Handle toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbarTitle = (TextView) mToolbar.findViewById(R.id.toolbar_title);
mToolbarIcon = (ImageView) findViewById(R.id.toolbar_icon);
setSupportActionBar(mToolbar);
initNavigationDrawer();
// load default fragment
if (savedInstanceState == null) {
onFragmentInteraction(NI_GOTO_SEARCH, null);
}
}
private void initNavigationDrawer() {
User currentUser = AccountManager.current(this);;
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListView = (ListView) findViewById(R.id.left_drawer);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
drawerItemList = new ArrayList<>();
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_USER, currentUser.getName(), currentUser.getEmail()));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_BLACK_NO_ICON, "Order History"));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_BLACK_NO_ICON, "Log Out"));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_WHITE_NO_ICON, "FAQ"));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_WHITE_NO_ICON, "About"));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_WHITE_NO_ICON, "Terms & Conditions"));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_WHITE_NO_ICON, "Privacy Policy"));
drawerItemList.add(new DrawerItem(DrawerItem.NAV_ITEM_TYPE_WHITE_NO_ICON, "Contact Us"));
CustomDrawerAdapter adapter = new CustomDrawerAdapter(this, R.layout.drawer_item_white_with_icon, drawerItemList);
drawerListView.setAdapter(adapter);
drawerToggle = new ActionBarDrawerToggle(
this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
}
@Override
public void onBackPressed() {
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.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
private void SelectDrawerItem(final int position) {
switch (position) {
case 0:
onFragmentInteraction(NI_DRAWER_GOTO_USER, null);
break;
case 1:
onFragmentInteraction(NI_DRAWER_GOTO_HISTORY, null);
break;
case 2:
onFragmentInteraction(NI_DRAWER_GOTO_LOGOUT, null);
break;
}
drawerListView.setItemChecked(position, true);
drawer.closeDrawer(drawerListView, false);
}
@Override
public void onFragmentInteraction(int interaction, Parcelable object) {
switch (interaction) {
case NI_GOTO_SEARCH:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
SearchFragment.newInstance(),
SearchFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_USER:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
ProfileFragment.newInstance(),
ProfileFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_HISTORY:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
HistoryFragment.newInstance(),
HistoryFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_LOGOUT:
AccountManager.logOut(this);
startActivity(new Intent(this, OpeningActivity.class));
finish();
break;
case NI_DRAWER_GOTO_FAQ:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
ProductFragment.newInstance(),
ProductFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_ABOUT:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
ProductFragment.newInstance(),
ProductFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_TERMS:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
ProductFragment.newInstance(),
ProductFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_PRIVACY:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
ProductFragment.newInstance(),
ProductFragment.TAG)
.commitAllowingStateLoss();
break;
case NI_DRAWER_GOTO_CONTACT:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
ProductFragment.newInstance(),
ProductFragment.TAG)
.commitAllowingStateLoss();
break;
default:
Toast.makeText(this, "No valid option", Toast.LENGTH_SHORT).show();
}
}
@Override
public void changeActionBarTitle(String title) {
getSupportActionBar().show();
getSupportActionBar().setDisplayShowTitleEnabled(false);
mToolbarTitle.setText(title);
mToolbarTitle.setVisibility(View.VISIBLE);
mToolbarIcon.setVisibility(View.GONE);
}
@Override
public void hideActionBar() {
getSupportActionBar().hide();
}
@Override
public void showActionBarAppIcon() {
getSupportActionBar().show();
getSupportActionBar().setDisplayShowTitleEnabled(false);
mToolbarTitle.setVisibility(View.GONE);
mToolbarIcon.setVisibility(View.VISIBLE);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SelectDrawerItem(position);
}
}
}
答案 0 :(得分:0)
由于这一行,你遇到了这个问题:
www.example/my-category/sample-post
这样改变:
String line = "calc.exe";
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
布尔值用于动画。
希望这有帮助。
答案 1 :(得分:0)
导致冻结的实际行是: drawerListView.setItemChecked(position,true);
我在搜索解释时只是评论了