Java ScrollView中的ListView和FrameLayout[错误]
我有一个主布局,包括2个布局,其中一个包含一个框架布局,内部有一个片段。另一个具有包含ListView的LinearLayout。以下是我的主要布局:Java ScrollView中的ListView和FrameLayout[错误],java,android,listview,Java,Android,Listview,我有一个主布局,包括2个布局,其中一个包含一个框架布局,内部有一个片段。另一个具有包含ListView的LinearLayout。以下是我的主要布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orien
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/map_layout" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></include>
<include layout="@layout/list_layout" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="6"></include>
</LinearLayout>
</ScrollView>
</LinearLayout>
但是ListView会一直抓住滚动条。这时,ScrollView出现错误,并将地图和le列表放在infite ScrollView的下方
我该怎么做才能让它工作呢?我想你可以用这样的东西
yourListView.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
我终于找到了问题的解决办法 我需要扩展我的listView,这样它就只能被ScrollView滚动了。下面是我找到的代码: Helper.java:
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
}
}
我在这里找到了这个解决方案:您试图做平台不支持的事情,这是它的一个很好的限制(它不是一个BUG),并且不起作用。你的问题是什么?我该怎么做才能让它工作呢?你没有把滚动视图放在ListVIew里面,这个平台不支持。试试CoordinatorLayout,它是滚动视图中的列表视图。但是我会看看如何在ScrollVIew中使用CoordinatorLayout.ListView,或者在ListView中使用ScrollVIew没有区别。它们都不支持嵌套滚动,而且嵌套滚动不支持它们。所以你应该做一些更详细的检查!
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
}
}
Helper.getListViewSize(myList);