导航抽屉代码如下:
private void ShowNavigationDrawer() {
// DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Populate the Navigtion Drawer with options
mDrawerList = (ListView) findViewById(R.id.left_drawer);
DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
mDrawerList.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer_white, R.string.drawer_open,
R.string.drawer_close);
// Drawer Item click listeners
mDrawerList
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItemFromDrawer(position);
}
});
}
其他使用的方法是:
/*
* Called when a particular item from the navigation drawer is selected.
*/
private void selectItemFromDrawer(int position) {
selectItem(position);
getSupportActionBar().setTitle(mNavItems.get(position).mTitle);
// Close the drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
选择要在一个活动中加载的正确片段:
/** Swaps fragments in the main content view */
private void selectItem(int position) {
switch (position) {
case 1:
currentFragment = new ABCFragment();
break;
case 2:
currentFragment = new SearchTabFragment();
break;
default:
currentFragment = new HomeFragment();
break;
}
currentFragment.setArguments(getIntent().getExtras());
fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_content_frame, currentFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
}
要在另一个片段中以编程方式从导航栏中选择(突出显示)特定项目,请调用以下方法,但它不起作用!
// Highlight the selected item
homeActivity.mDrawerList.setItemChecked(position, true);
问题出在哪里?任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
您可以尝试使用新的NavigationView而不是ListView来实现导航抽屉。像这样:
<android.support.design.widget.NavigationView
android:id="@+id/nv_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:itemIconTint="@color/navigation_item_selector"
app:itemTextColor="@color/navigation_item_selector"
app:itemBackground="@android:drawable/screen_background_light_transparent"
android:layout_gravity="start" />
您可以使用菜单.xml文件填充NavigationView。然后,您可以轻松地以编程方式选择这样的项目:
mNavigationView.setCheckedItem(R.id.my_item_1);
并且drawable选择器将以正确的方式处理突出显示的工作。例如,这是我的选择器(navigation_item_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/red" android:state_checked="true" />
<item android:color="@color/red" android:state_pressed="true" />
<item android:color="@color/black" />
</selector>
PS我知道它不是你想要的解决方案,但它是你可能会发现有用的解决方案。
答案 1 :(得分:1)
因为突出显示意味着可以根据需要为其提供背景颜色。如果您不希望它出现在项目的点击事件中,而是另外想要它,则必须在导航抽屉适配器上手动提供。像这样
public void onBindViewHolder(MyViewHolder holder, int position) {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
if(position==0)
{
holder.llnavdrawer.setBackgroundResource(R.color.black);
}
else if(position==2)
{
holder.llnavdrawer.setBackgroundResource(R.color.grey);
}
else if(position==4)
{
holder.llnavdrawer.setBackgroundResource(R.color.green);
}
else if(position==6)
{
holder.llnavdrawer.setBackgroundResource(R.color.black);
}
}
你可以问你想要的东西不是这个。 祝你好运!!