我有一个导航抽屉,我使用选择器来获取列表中的高位项目
<item
android:state_activated="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
问题在于我覆盖了反对以将我的片段更改为我的主元素,如下所示:
@Override
public void onBackPressed()
{
onNavigationDrawerItemSelected(0);
onSectionAttached(1);
restoreActionBar();
现在当我按回它时会更改片段,但突出显示的项目会保留在我上次点击的位置。我如何才能更改高光位置?与somethig一样改变文本以某种方式从代码状态,但我用适配器创建导航抽屉元素...(我使用Android Studio示例)
@Override
public void onNavigationDrawerItemSelected(final int position) {
// update the main content by replacing fragments
final FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, KedvencekFragment.newInstance(position + 1))
// .addToBackStack(null)
.commit();
if(position==0)
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
fragmentManager.beginTransaction()
.replace(R.id.container, KedvencekFragment.newInstance(position + 1))
// .addToBackStack(null)
.commit();
}
}, 250);
}
和`public void onSectionAttached(int number){ 开关(号码){ 情况1: mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
// mTitle = getString(R.string.title_section5);
break;
case 6:
mTitle = getString(R.string.title_section6);
break;
}
}`
答案 0 :(得分:0)
修改强>
您可以在自己的活动中访问NavigationDrawerFragment
,只需要selectItem(int position)
公开NavigationDrawerFragment
方法,就像这样:
...
publicvoid selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
...
然后在你的onNavigationDrawerItemSelected
方法中:
@Override
public void onNavigationDrawerItemSelected(final int position) {
// update the main content by replacing fragments
final FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, KedvencekFragment.newInstance(position + 1))
// .addToBackStack(null)
.commit();
if(position==0)
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
fragmentManager.beginTransaction()
.replace(R.id.container, KedvencekFragment.newInstance(position + 1))
// .addToBackStack(null)
.commit();
}
}, 250);
}
navigationDrawerFragment.selectItem(position);
}
答案 1 :(得分:0)
@Override
public void onBackPressed()
{
if(mTitle!=getString(R.string.title_section1)) {
onNavigationDrawerItemSelected(0);
onSectionAttached(1);
mNavigationDrawerFragment.selectItem(0);
restoreActionBar();
}
else{
super.onBackPressed();
}
}