1. 程式人生 > >ScrollView巢狀ListView,顯示不全和位置不是頂部的解決辦法

ScrollView巢狀ListView,顯示不全和位置不是頂部的解決辦法

(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();