Android – Scrollview in percentrelativelayout 23.2.1 has incorrect zero height

I have a layout, which contains a Scrollview with LinearLayout and its children: a percentrelativelayout 23.2.1, another lower LinearLayout and another percentrelativelayout. In the middle of the LinearLayout, I have a recyclerview (LinearLayout manager, vertical). You can see an example below

At runtime, when I fill the data with recycleview, its size will increase because I start with an empty data. Once its height changes beyond the screen size, the image above the car (id = car), whose percentage value as its height, will completely disappear from the screen. When I scroll up and down the screen, I reach the top and bottom edges and can't see the image

I also have percentage values in another position in this layout, which define margintop. These edges also disappear. The percentage values related to width work normally. The Java code does not include image visibility changes or adjust the margins

I'm not sure whether this problem is related to a specific version of the support library. I built this screen while publishing this library. In addition, I didn't see this problem on marshmallow, only in lolipop and below. I will be grateful for how to solve this problem

<!-- a container for all views that should scroll -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- the first block of views - they need percents -->
    <android.support.percent.PercentRelativeLayout
        android:id="@+id/carHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/car"
            android:layout_width="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/car"
            app:layout_heightPercenet="25%"/>

        <ImageView
            android:id="@+id/sliderBg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/car"
            android:layout_centerHorizontal="true"
            android:src="@drawable/slider_bg"/>

    </android.support.percent.PercentRelativeLayout>

    <!-- a middle block of views, they don't need percents -->
     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

         <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </LinearLayout>

    <!-- another block of views, they need percents -->
    <android.support.percent.PercentRelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <!-- some other views --> 

    </android.support.percent.PercentRelativeLayout>

</LinearLayout>

UPDATE

I also saw this problem in the support library 23.3.0, not only when I filled the recyclerview, but also every time the Scrollview exceeded the screen height

resolvent:

Unfortunately, percentage layouts are not applicable to Scrollview before M. the reason is that they depend on the size prompt provided in the measurement step. Before m, most layouts provide size prompt 0 when sending unspecified measurement specifications

You can try to fix it by creating your own Scrollview subclass and overriding measurechild and measurechildwithmargins (fortunately both are protected) to provide size hints. Source - plus.google.com

You can use this customscrollview to make the percentrelativelayout work correctly

public class CustomScrollView extends ScrollView {


public CustomScrollView(Context context) {
    super(context);
}

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = 0;
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    if (width > height) {
        size = height;
    } else {
        size = width;
    }
    setMeasuredDimension(size, size);
}
}

The source is percentrelativelayout inside Scrollview

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>