我有一个主要的布局,包括2个布局,一个包含一个框架布局与Fragment里面。另一个是包含ListView的LinearLayout。这是我的主布局:
<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>
第一个Fragment包含一个map,第二个Fragment包含一个带有自己的listAdapter的自定义ListView。
这是我的问题,我知道把ListView放到ScrollView中有点棘手。但是在listfragment。java的onCreateView中我写了这个:
list.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
但是ListView保持滚动。一会儿,ScrollView的bug,把地图和列表放到无限ScrollView
我怎么做才能使它工作呢?
我终于找到解决问题的办法了。
我需要扩展我的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);
}
}
,要实现它,只需要这一行:
Helper.getListViewSize(myList);
我找到了这个解决方案:http://www.androidhub4you.com/2012/12/listview-into-scrollview-in-android.html