如果您希望 ListFragment 中的标准 ListView 行为,您可以将列表项行布局设置为使用 ?android:attr/activatedBackgroundIndicator 作为背景。像这样:
ListItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator" <!--This is the important bit-->
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight" />
<TextView
android:text=""
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
ListFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:tools="http://schemas.android.com/tools"
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@drawable/divider"
android:dividerHeight="0.5dp" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No data" />
</LinearLayout>
在您的 ListFragment 片段代码中,使用自定义布局(这是单声道代码,但不要绝望):
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
base.OnCreateView(inflater, container, savedInstanceState);
View view = inflater.Inflate(Resource.Layout.ListFragment, container, false);
//... do stuff
}
在您要创建适配器的代码中的某个位置,使用 ListItem 布局。在这种情况下,SimpleAdapter 将数据映射到图标和文本视图。 ListItem.xml (?android:attr/activatedBackgroundIndicator) 中的背景使项目的行为与您在普通列表中看到的一样:
var list = new List<IDictionary<string, object>>(listOfStuff.Count);
foreach (AMap map in listOfStuff) {
var dictionary = new JavaDictionary<string, object> {
{"text", map.Text}, {"icon", Resource.Drawable.SomeIcon}
};
list.Add(dictionary);
}
SimpleAdapter _adapter = new SimpleAdapter(Activity, list,
Resource.Layout.ListItem,
new string[] { "text", "icon" },
new int[] { Resource.Id.text,
Resource.Id.icon });