-2

我已经设置了一个 Activity 和一个 Fragment,在布局中我有一个 ListView,我给它一个任意名称并通过 findViewById() 访问。一切正常,直到我注意到滚动不适用于 ListView。

SO上有很多关于ListView和缺乏滚动的线程。我花了比我愿意承认尝试一个又一个可能的解决方案更多的时间。没有任何效果,直到我偶然发现了解决方案,我将在对这个问题的回答中进行描述。

编辑:

我已经删除了我发布到我自己的问题的答案,我声称您必须使用 ListFragment(或 SherlockListFragment)才能滚动 ListView 工作。

感谢 Zackehh9lives,我确定我的说法是错误的。

他回答中的关键短语是他展示的代码树桩在 onCreateView() 方法中。经过一番试验,我认为情况如下:

如果您使用 ListFragment(或 SherlockListFragment),则必须在 onActivityCreated() 方法中设置 ListView 小部件,并且 ListView 滚动有效。

如果您使用 Fragment(或 SherlockFragment),您可以在 onActivityCreated() 或 onCreateView() 中设置 ListView,但如果您在 onActivityCreated() 中设置,则滚动不起作用。

那是我的问题——我正在使用 SherlockFragment 并在 onActivityCreated() 中设置 ListView。

4

1 回答 1

1

我有一个ListViewSherlockFragment它滚动得很好。不确定是什么问题?也许您只是在某处有不正确的地方(可能想发布您的代码?)。下面是我的 ListView 和 XML:

MyFragment.java:

// Find the ListView we're using
list = (ListView)view.findViewById(R.id.listView);
// Set the vertical edges to fade when scrolling
list.setVerticalFadingEdgeEnabled(true);

// Create a new adapter
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.title, questions);
// Set the adapter to the list
list.setAdapter(adapter);

// Set on item click listener to the ListView
list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // Do stuff
    }
});

所有这些代码都是我在 Fragment 中与我的 ListView 相关的,它在我的onCreateView(). 下面是它的 XML。

列表项.xml:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_toLeftOf="@+id/arrow"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="@string/title"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/title"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:text="@string/date"
        android:textColor="#7F7F7F"
        android:textSize="12sp" />
</RelativeLayout>

<ImageView
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:contentDescription="@string/app_name"
    android:src="@drawable/arrow_next" />

列表视图.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:paddingTop="-2dp" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

如果您发布您的代码,我们就有更好的机会提供帮助:)

于 2013-08-03T04:08:11.367 回答