Ayu*_*yal 9 android listview android-listview android-fragments android-listfragment
我已经发布过几次同样的问题,但还没有解决.我有一个ListFragment,我想突出显示列表中的所选项目.我得到了使用"选择器"的建议.我不明白如何使用这个选择器.我的ListFragment班级是:
// Create an adapter with list of stores and populate the list with
// values
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, StoreList);
setListAdapter(adapter);
mDbHelper.close();
}
/*
* (non-Javadoc)
*
* Handles the event when an item is clicked on left pane, performs action
* based on the selection in left pane
*
* @see android.app.ListFragment#onListItemClick(android.widget.ListView,
* android.view.View, int, long)
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String selectedStore = (String) getListAdapter().getItem(position);
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
v.setBackgroundColor(getResources().getColor(R.color.BLUE));
// passes selectedStore to detail fragment
fragment.setText(selectedStore);
// getItemList(selectedStore);
}
Run Code Online (Sandbox Code Playgroud)
使用setBackground永久设置颜色,但我希望它在选择另一个项目时消失.我理解如何使用选择器,ListView但在我的情况下,如果我没有为其定义任何xml Listview,那么我将如何使用"选择器"?我使用的android.R.layout.simple_list_item_1是预定义的.
以下为我工作:
RES /颜色/ menu_highlight.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/red" />
<item
android:state_selected="true"
android:drawable="@color/red" />
<item
android:drawable="@color/white" />
</selector>
Run Code Online (Sandbox Code Playgroud)
RES /值/ colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="red">#FF0000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
res/layout/menuitem.xml : :(列表中每个项目的XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/textmenu"
android:layout_height="wrap_content"
android:text="text"
android:textColor="#FFFFFF"
android:background="@color/menu_highlight"
android:visibility="visible"
android:layout_width="fill_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
最后,在ListFragment类中,添加View previous并将以下代码添加到onlistitemclick函数中.(在ListFragment中提到:突出显示所选行)
public class MenuListFragment extends ListFragment{
View previous;
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Logic to highlight selected item
previous.setSelected(false);
v.setSelected(true);
previous=v;
}
}
Run Code Online (Sandbox Code Playgroud)
来自你的ListView电话setChoiceMode(ListView.CHOICE_MODE_SINGLE).然后,只要您想突出显示所选项目,请致电setItemChecked(index, true).
我没有得到我想要的,所以我继续挖掘并提出了一个“便宜”的解决方案,这可能不是最佳实践,但可以完成工作。我希望该项目在选择时突出显示,并且当从 listFragment 中选择其他项目时颜色应该消失。这对我有用-我定义了一个静态View V;
并初始化它V = new View(getActivity());
然后在我的内部
onListItemClick(ListView l, View v , int 位置, 长 id)
V.setBackgroundResource(0);
v.setBackgroundResource(R.color.BLUE);
V = v;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13797 次 |
| 最近记录: |