ListView控件中滚动查看我的滚动型移动到列表视图的顶部。我如何prevent呢? [英] ListView in Scroll View my scrollview moves to top of listview. How do I prevent this?
问题描述
我有我的滚动视图列表视图下几乎是板面的滚动之前,但一旦我的列表视图被填充的滚动型移动到列表视图的顶部。我怎样才能解决这个问题/ prevent这种情况的发生?
I have a listview in my scroll view underneath almost a page worth of scroll before that but once my listview gets populated the scrollview moves to the top of the list view. how can I fix this/prevent this from happening?
滚动视图的XML:
<ScrollView
android:id="@+id/tvscrollview"
android:layout_marginTop="8.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
layout="@layout/a" />
<include
layout="@layout/b" />
<include
layout="@layout/c" />
<include
layout="@layout/d" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
我试着做 sv.fullScroll(ScrollView.FOCUS_UP); 和所有的东西,以我的滚动型,但它不工作
I've tried doing sv.fullScroll(ScrollView.FOCUS_UP); and all that stuff to my scrollview but it doesnt work
推荐答案
首先,很可能你已经听说过这件事,但以防万一:您永远不应该把的ListView
A 滚动型
,因为的ListView
本身就已经得到了一个<$ C $内C>滚动型键,这样的设计不利于整个的ListView
的想法。如果你确信你要使用它,也许有更好的方法来使用,你可能需要以某种方式简化你的code。
Firstly, probably you've already heard about it, but just in case: You should never put a ListView
inside a ScrollView
, as ListView
itself already has got a ScrollView
and this design goes against the whole ListView
idea. If you're convinced you have to use it, probably there's a better approach to use and you may need to simplify your code somehow.
现在,即使你仍然想使用类似的东西,你可以使用这样的:
Now, even if you still want to use something like that, you may use something like this:
package your.package.name;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ScrollingListView {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
您只需通过 .setAdapter设置你的适配器()
事后调用 ScrollingListView.setListViewHeightBasedOnChildren(your_listview)
。这应该相应REDIMENSION你的窗口,你的的ListView
的高度。
You simply set your adapter via .setAdapter()
and afterwards call ScrollingListView.setListViewHeightBasedOnChildren(your_listview)
. This should redimension your window accordingly to your ListView
height.
----编辑----
这很可能是最丑陋的解决方法,但尝试以下操作:
That will be probably the ugliest workaround, but try doing the following:
ScrollView sv = (ScrollView) findViewById(R.id.your_scrollview);
sv.setEnabled(false);
// Populate your ListView
sv.setEnabled(true);
这篇关于ListView控件中滚动查看我的滚动型移动到列表视图的顶部。我如何prevent呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!