(1)首先写一个listview的页面,用ScrollView进行嵌套,注意ScrollView里面只能有一个布局属性,多个可以用Linearout进行包含
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.a10109.demo.activity.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="30dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/lin_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:orientation="vertical"> <com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="150dp" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:scaleType="fitXY" app:riv_border_color="#00000000" app:riv_border_width="2dp" app:riv_corner_radius="10dp" app:riv_mutate_background="true" app:riv_oval="false" /> <android.support.v7.widget.RecyclerView android:id="@+id/recycle" android:layout_width="match_parent" android:layout_height="120dp"> </android.support.v7.widget.RecyclerView> </LinearLayout> <LinearLayout android:id="@+id/lin_bottom" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="2dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="25dp" android:layout_marginBottom="3dp" android:padding="1dp" android:text="选择商品" /> <View android:layout_width="match_parent" android:background="#8f8f8f" android:layout_marginBottom="3dp" android:visibility="gone" android:layout_height="1dp"/> <!--<android.support.v7.widget.RecyclerView--> <!--android:id="@+id/list"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content">--> <!--</android.support.v7.widget.RecyclerView>--> <com.example.a10109.demo.CustomView.MyListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:divider="#8f8f8f" android:dividerHeight="1dp" android:scrollbars="none"/> </LinearLayout> </LinearLayout> </ScrollView> <RelativeLayout android:id="@+id/to_pay" android:background="#b00" android:gravity="center" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="45dp"> <TextView android:id="@+id/tv_paynumber" android:gravity="center" android:textColor="@color/white" android:text="去支付:" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </RelativeLayout>
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的所有数据,但是发现数据多时listview会到最顶部,解决办法有如下5中办法。
1.myScrollView.smoothScrollTo(0,20); 需在listview数据加载完成后调用
2.在代码里去掉listview的焦点 ; listview.setFocusable(false);
3.Listview外套一层LinearLayout ;
4.跟EditText一样,在父元素的属性下面下下面这两行即可 ;
android:focusableInTouchMode=”true”
android:focusable=”true”
5.最开始的时候让最上面其中一个控件获得焦点,滚动条自然就到顶部去了,如下:
txtBaseMsg.setFocusable(true);
txtBaseMsg.setFocusableInTouchMode(true);
txtBaseMsg.requestFocus();