我在ScrollView中有一个ListView。
要在ListView和ScrollView中启用滚动,我实现了这个方法,我从StackOverFlow上的其他一些问题中得到了: -
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
问题在于,因为ListViewHeight已经完全无法调用ListView上的OnScroll方法。我想在用户滚动时执行一些代码(一些异步加载图像)。有没有解决方法?
我的布局xml文件: -
<ScrollView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:background="#ffffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="info"
android:id="@+id/njnjj"/>
<ListView
android:id="@+id/gjghjgh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:visibility="visible"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
我知道在ScrollView中使用ListView是一种很好的做法根本不是,但我需要一种解决方法。