ScrollView doesn't show the upper part of XML

2020-07-22 06:41发布

问题:

My XML was working perfectly, it has a listview, set of textviews, imageviews and buttons. Then I added a scroll view to the entire layout. Listview stopped functioning and then I added a custom class in order t o make it work. Now everything is working as intended, but the issue is that the top part of the layout is not visible when the xml loads, I have to scroll up to see the top part of it. My issue is same as this but still the provided solutions don't work: ScrollView doesn't show top part This is the XML file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_default"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/navigation_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/navigation"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/app_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:textColor="#057afb"
                    android:textSize="19sp"
                    android:textStyle="bold" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/navigation_layout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/navigation_layout"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:paddingTop="10dp" >



                <ImageView
                    android:id="@+id/app_icon"
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="23dp"
                    android:layout_marginTop="14dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_launcher" />

                <Button
                    android:id="@+id/btn_install"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignTop="@+id/app_icon"
                    android:layout_marginRight="36dp"
                    android:background="@drawable/list_apps_install_button_selector" />

                <TextView
                    android:id="@+id/txt_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/app_icon"
                    android:layout_below="@+id/app_icon"
                    android:text="desription goes here"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/txt_desc_long"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/app_icon"
                    android:layout_below="@+id/app_icon"
                    android:text="desription goes here"
                    android:textAppearance="?android:attr/textAppearanceSmall" 
                    android:visibility="gone"/>

                <TextView
                    android:id="@+id/txtMore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="14dp"
                    android:text="...More"
                    android:textColor="#057afb" />

            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/navigation_layout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/navigation_layout1"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="fill_parent"
                    android:layout_height="2dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/txt_desc"
                    android:layout_marginTop="15dp"
                    android:background="@drawable/separator_app_details" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/information"
                    android:layout_below="@+id/information"
                    android:layout_marginLeft="50dp"
                    android:layout_marginTop="5dp"
                    android:text="@string/version"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#929292" />

                <TextView
                    android:id="@+id/txt_version"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView3"
                    android:layout_alignBottom="@+id/textView3"
                    android:layout_marginLeft="22dp"
                    android:layout_toRightOf="@+id/textView3"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/textView3"
                    android:layout_below="@+id/textView3"
                    android:text="@string/uploader"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#929292" />

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView5"
                    android:layout_below="@+id/textView5"
                    android:text="@string/updated"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#929292" />

                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/information"
                    android:layout_below="@+id/textView7"
                    android:text="@string/size"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#929292" />

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/textView9"
                    android:layout_below="@+id/textView9"
                    android:text="@string/compatibility"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#929292" />

                <TextView
                    android:id="@+id/txt_uploader"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/textView7"
                    android:layout_alignLeft="@+id/txt_version"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/txt_updated"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_uploader"
                    android:layout_below="@+id/txt_uploader"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/txt_size"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/textView11"
                    android:layout_alignLeft="@+id/txt_updated"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/txt_compat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView11"
                    android:layout_alignBottom="@+id/textView11"
                    android:layout_alignLeft="@+id/txt_size"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <TextView
                    android:id="@+id/information"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="14dp"
                    android:layout_marginTop="19dp"
                    android:text="@string/txt_info"
                    android:textColor="#000000"
                    android:textSize="19sp" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/navigation_layout3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/navigation_layout2"
            android:layout_marginTop="15dp"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/older_versions"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="14dp"
                    android:layout_marginTop="10dp"
                    android:text="@string/old"
                    android:textColor="#000000"
                    android:textSize="19sp" />

                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/older_versions"
                    android:divider="@null" >
                </ListView>
            </RelativeLayout>
        </LinearLayout>

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/navigation_layout3"
            android:background="@drawable/separator_app_details" />
    </RelativeLayout>

</ScrollView>

This is the class that was written to make both scroll view and the list view function at the same time:

public class Utility {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

}

回答1:

Put this in the OnCreate() method:

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview)); scrollview.post(new Runnable() {
  @Override public void run() {
    scrollview.fullScroll(ScrollView.FOCUS_UP);
  }
});

The code above provides you with a way to force the scroll of the ScrollView at the upper position. This behavior can be caused even by a weight attribute in a some layouts etc. The "debugging" of this will take more time that to apply the solution above and there isn't a guarantee that you can know each time which disturbs your initial scroll position.