如何解决滚动视图中的ListView我的滚动视图移到列表视图的顶部我该如何预防?
首先,您可能已经听说过它,但以防万一:,因为ListView
它本身已有一个,ScrollView
并且此设计与整个ListView
想法背道而驰。如果您确信必须使用它,则可能有更好的使用方法,并且可能需要以某种方式简化代码。
现在,即使您仍想使用类似的东西,也可以使用类似的东西:
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)
。这将根据您的ListView
高度重新定义您的窗口。
这可能是最丑陋的解决方法,但是请尝试执行以下操作:
ScrollView sv = (ScrollView) findViewById(R.id.your_scrollview);
sv.setEnabled(false);
// Populate your ListView
sv.setEnabled(true);
解决方法
在此之前,我的滚动视图下面几乎有一个值得滚动的页面,下面有一个列表视图,但是一旦填充了列表视图,滚动视图就会移到列表视图的顶部。我该如何解决/防止这种情况发生?
滚动查看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); 和所有这些东西到我的scrollview,但它不起作用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。