gpt4 book ai didi

android - SlidingMenu 无法突出显示所选项目

转载 作者:行者123 更新时间:2023-11-30 02:50:44 26 4
gpt4 key购买 nike

我正在为 Android 使用 SlidingMenu,我有一个像这样的“菜单 fragment ”:

public class MenuListFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] items = getResources().getStringArray(R.array.menu);
ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, items);
setListAdapter(colorAdapter);
getListView().setSelector(R.drawable.menu_selector);
}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
switch (position) {
case 0:
etc...
}
if (newContent != null)
switchFragment(newContent);
v.setSelected(true);
lv.setSelected(true);
}

private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;

MainActivity activity = (MainActivity) getActivity();
activity.switchContent(fragment);
}
}

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">

<!--<item android:drawable="@android:color/transparent" android:state_pressed="true"/>-->
<item android:drawable="@color/white" android:state_selected="true"/>
<!--<item android:drawable="@color/red" android:state_activated="true"/>-->
<item android:drawable="@color/black" />

</selector>

和列表布局:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding"
android:choiceMode="singleChoice"/>

单击菜单中的一项后,菜单将隐藏(如预期的那样)。但是当菜单再次显示时,没有更多的选择项。

有没有办法让项目保持选中状态?

更新

根据 Shereef Marzouk 的建议,我进行了更改:

public class MenuListFragment extends ListFragment {

private View selectedView;

...

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
switch (position) {
// stuff
}
if (selectedView != null) selectedView.setBackgroundColor(getResources().getColor(R.color.aqincolor));
v.setBackgroundColor(getResources().getColor(R.color.espresso));
selectedView = v;
if (newContent != null)
switchFragment(newContent);
}

...
}

在这种情况下它可以工作,但我不明白有一个即使调用 super.onActivityCreated(savedInstanceState); 也不会切换的 menu_selector.xml

最佳答案

这是我在申请中所做的:

final Class<?> currentClass = getActivity.getClass();
switch (position) {
case 0: //Menu
cls = MainNavigationActivity.class;
if (currentClass.equals(cls)) {
view.setBackgroundColor(parent.getContext().getResources().getColor(R.color.selectedMenuBackground));
}
break;
}

初始化菜单时,检查您初始化了哪个类,并根据该类更改 BG 颜色

关于android - SlidingMenu 无法突出显示所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com