在viewholder中查看不更新(不占用高度)

时间:2016-09-23 08:29:59

标签: android android-layout android-viewholder viewstub

我有一个配置为wrap_content的视图。在该视图中,如果用户未登录,我会显示一个视图。此视图会以ViewStub的形式夸大。

在屏幕上的第一个单元格中,一切都很好。但是,如果我滚动到屏幕外的单元格,那就错了。这个观点没有达到他的高度。

在第三个单元格中,android:id="@+id/background"的{​​{1}}(View_NoLoggedInView中的ViewStub未达到他的身高。

我删除了不相关的代码,使类和布局更具可读性。

有没有人知道为什么第三个小区只有在他离开屏幕然后再次进入时才会更新?我是如何解决这个问题的?

在这里你会发现视觉问题为gif:

enter image description here

查看者 AXML布局:

    <?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>

膨胀 View_NoLoggedInView viewstub布局Axml代码:

    <?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>

以下是

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
}

}

0 个答案:

没有答案