0

有什么办法可以将ListViewin aSherlockListFragment从默认更改为R.id.list?例如,我有一个自定义列表,它覆盖了ListView定义为低于线性布局的 xml(列表)中的默认值。我该如何做setListAdapter才能使用它ListView而不是默认的 android ListView

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_view_panel"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/toggleLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <ToggleButton
            android:id="@+id/toggle_button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ToggleButton
            android:id="@+id/toggle_button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ToggleButton
            android:id="@+id/toggle_button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toggleLL" />

</RelativeLayout>

分段:

public class ProblemFragment extends SherlockListFragment
{
    private SeparatedListAdapter list;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getSherlockActivity().setContentView(R.layout.apps_layout);

        list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));

        setListAdapter(list); //Sets the list to the default list, not the overwritten list specified in the xml
    }
}
4

1 回答 1

1

只需更改您的布局 XML 以使用android:id="@android:id/list".

ListActivity 和 ListFragment 依赖于具有 id 的 ListView android.R.id.list,它在 SDK 中公开公开(并且可以通过 XML 访问@android:id/list)。如果您查看这些类中的任何一个的文档,都提到使用您自己的布局需要您为 ListView 提供此 id 才能使其正常工作。

于 2013-08-13T15:08:41.187 回答