1、前言
当你看到这篇文章的时候,其实你就已经错了,因为ScrollView 中嵌套 List 或 RecyclerView 的做法官方明确禁止。除了开发过程中遇到的各种视觉和交互问题,这种做法对性能也有较大损耗。ListView 等 UI 组件自身有垂直滚动功能,也没有必要在嵌套一层 ScrollView。目前为了较好的 UI 体验,更贴近 Material Design 的设计,推荐使用 NestedScrollView。
在APP开发中会用到listview和某个Linearout一起滑动的情况,有两种实现方式;一是listview嵌套Scrollview,二是给listview加头,这里就都说一下。
2、listview嵌套Scrollview问题一:listview数据只显示一行怎么办 ?
(1)首先写一个listview的页面,用ScrollView进行嵌套,注意ScrollView里面只能有一个布局属性,多个可以用Linearo
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:focusable="true"
android:layout_height="match_parent">
<com.bigkoo.convenientbanner.ConvenientBanner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/building_advert"
android:layout_width="match_parent"
android:layout_height="200dp"
app:canLoop="true" />
<LinearLayout
android:layout_width="match_parent"
android:background="@color/gray_background"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:text="当前区域:"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@color/gray_font"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:text="和平区"
android:textColor="@color/gray_font"
android:gravity="center_vertical"
android:layout_height="match_parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:src="@drawable/dropdown_gray"
/>
</LinearLayout>
<ListView
android:id="@+id/main_building_listivew"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:listSelector="#00000000" >
</ListView>
</LinearLayout>
</ScrollView>
(2)、页面填写完成后,需要给listview加载数据,加载完成后需要手动计算listview的高度,不然只能显示一行数据
/*
* 当ScrollView 与 LiseView 同时滚动计算高度的方法
* 设置listview 的高度
* 参数:listivew的findviewbyid
* */
public static void setListViewHeightBasedOnChildren(ListView listView) {
try{
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}catch (Exception e){
Helper.saveFileLog("setListViewHeightBasedOnChildren___"+e.toString());
}
}
3、问题二:加载数据时,会显示到listview的顶部,而不是整个页面的最顶部
解决办法有如下5中办法:
-
myScrollView.smoothScrollTo(0,20);
需在listview数据加载完成后调用
-
在代码里去掉listview的焦点
lv.setFocusable(false);
-
Listview外套一层LinearLayout
-
跟EditText一样,在父元素的属性下面下下面这两行即可
android:focusableInTouchMode=”true”
android:focusable=”true” -
最开始的时候让最上面其中一个控件获得焦点,滚动条自然就到顶部去了,如下:
txtBaseMsg.setFocusable(true);
txtBaseMsg.setFocusableInTouchMode(true);
txtBaseMsg.requestFocus();