6

我有一个配置为wrap_content. 如果用户未登录,我会在该视图中显示一个视图。该视图膨胀为ViewStub.

在屏幕上的第一个单元格中,一切都很好。但是,如果我滚动到屏幕外的单元格,就会出错。视野不占他的高度。

在第三个单元格android:id="@+id/background"View_NoLoggedInView(中的那个ViewStub)没有占用他的高度。

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

有谁知道为什么第三个单元格只有在他离开屏幕然后再次进入时才会更新,我该如何解决这个问题?

在这里,您会发现 gif 格式的视觉问题:

在此处输入图像描述

Viewholder 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
}

}

4

0 回答 0