View in viewholder does not update (doesn't take height)
Asked Answered
L

0

6

I have a viewholder that is configured to wrap_content. In that viewholder I show a view if the user is not logged in. This view is inflated as ViewStub.

In the first cells on the screen everything is fine. But if I scroll to the cell that is outside the screen it goes wrong. The view does not take his height.

In the third cell the android:id="@+id/background" of the View_NoLoggedInView (the one in the ViewStub) does not take his height.

I removed the irrelevant code to make the classes and layouts more readable.

Does anyone know why the third cell only updates if he leaves the screen and then enters again and how I can solve this?

Here you find the visual problem as gif:

enter image description here

Viewholder AXML layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/flickrList_red">
    <ViewStub
        android:id="@+id/noLoggedInViewInclude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/View_NoLoggedInView" />
</RelativeLayout>

Inflated View_NoLoggedInView viewstub layout Axml code:

    <?xml version="1.0" encoding="utf-8"?>
<Kvo.Droid.View_NoLoggedInView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">
    <View
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.75"
        android:background="@android:color/black" />
    <RelativeLayout
        android:id="@+id/whiteCard"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/whitecard">
        <ImageView
            android:id="@+id/lockIcon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:scaleType="centerInside"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:src="@mipmap/lock" />
        <MobileFans.Base.Droid.BaseTextView
            android:id="@+id/contentText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_toRightOf="@id/lockIcon"
            android:gravity="center_vertical"
            android:layout_centerVertical="true"
            android:textColor="@color/flickrList_red"
            android:text="Deze content is enkel beschikbaar zodra je ingelogd bent." />
    </RelativeLayout>
</Kvo.Droid.View_NoLoggedInView>

Here is the class:

public abstract class BasePollViewHolder : BaseTimeLineDateViewHolder, View.IOnLayoutChangeListener
{
    #region variables
    bool hasLayoutListener = false;
    #endregion

    #region properties
    public abstract int NotLoggedInViewResourceId { get; }
    public abstract int BackgroundResourceId { get;}
    public BaseNoLoggedInView NoLoggedInView { get; set; }

    protected PollTimelineVO PollObject
    {
        get
        {
            return BaseTimelineObject as PollTimelineVO;
        }
    }
    #endregion

    #region constructors
    public BasePollViewHolder(Android.Views.View itemView) : base(itemView)
    {

    }
    #endregion

    #region public methods
    #region overrided methods

    public override void SetData(object data)
    {
        base.SetData(data);

        if (!hasLayoutListener)
        {
            hasLayoutListener = true;
            ItemView.AddOnLayoutChangeListener(this);
        }

        PollObject.LoginRequired = true;//HACK

        //NO LOGGED IN VIEW
        showLock(PollObject.LoginRequired);
    }

    void showLock(bool show) { 

        if (NoLoggedInView==null && show)
        {
            ViewStub stub = (ViewStub)ItemView.FindViewById(NotLoggedInViewResourceId);
            NoLoggedInView = (BaseNoLoggedInView)stub.Inflate();
            NoLoggedInView.BringToFront();
            NoLoggedInView.ContentText.Text = MyAppController.GetCopy(CopyConstants.TIMELINE_MUSTLOGIN_MESSAGE);

        } else if(NoLoggedInView != null && show)
        {
            NoLoggedInView.Visibility = ViewStates.Visible;
        } else if(NoLoggedInView != null)
        {
            NoLoggedInView.Visibility = ViewStates.Invisible;
        }
    }

    protected override void Dispose(bool disposing)
    {
        ItemView.RemoveOnLayoutChangeListener(this);
        hasLayoutListener = false;
        base.Dispose(disposing);
    }

    public void OnLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
    {
        if (NoLoggedInView != null)
        {
            BzLogging.Log(String.Format("LOG: width: {0}, height: {1}", v.Width, v.Height));
            ItemView.RemoveOnLayoutChangeListener(this);
            hasLayoutListener = false;
            updateNoLoggedInView(v);
        }
    }


    #endregion
    #endregion

    #region private methods

    void updateNoLoggedInView(View v) { 
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)NoLoggedInView.LayoutParameters;
        lParams.Height = v.Height;
        NoLoggedInView.LayoutParameters = lParams;
        NoLoggedInView.RequestLayout();
    }

    #endregion
}

}

Lathing answered 23/9, 2016 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.