RecyclerView始终显示项目布局的可见性true

时间:2018-08-02 08:53:47

标签: c# android xamarin xamarin.android

我正在使用RecyclerView显示项目列表。在项目单元格(用cardview包装)中,我有2个LinearLayouts,1个是Visible,第二个可见性是Gone。 当我单击任何项​​目并检查2LinearLayout可见性时,即使可见性设置为“消失”,它也会始终显示可见。 下面的代码我正在使用

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
    vh.ItemView.Click += async(sender, e)=>
    {                
        if (vh.linearAbLayout != null)
        {
            if (vh.linearAbLayout.Visibility == ViewStates.Visible)
            {
                //Always executing this part
                vh.linearAbLayout.Visibility = ViewStates.Gone;
            }
            else
            {
                //Control not coming here
                vh.linearAbLayout.Visibility = ViewStates.Visible;
            }
        }
    };
}

axml单元文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="3dp">
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="45.0dp"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:paddingTop="5dp"
        android:layout_weight="1">          
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=" Student name"
        android:id="@+id/lblStudentName"
        android:textColor="@color/azure"
        android:layout_marginTop="5dp" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="fill_parent"
        android:id="@+id/atReasonLayout"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:background="@color/silver"
        android:visibility="gone"
        android:layout_height="100dp">
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="6dp"
        android:textColor="@color/mainColor"
        android:textSize="14sp"
        android:text="Select reason"
        android:layout_marginLeft="5dp" />
    </LinearLayout>
</LinearLayout>

ViewHolder类

public class Holder : RecyclerView.ViewHolder
{
    public LinearLayout linearAbLayout
    {
      get;
      set;
    }
    public Holder(View itemView, Action<View> listener)
        : base(itemView)
    {           
        linearAbLayout = itemView.FindViewById<LinearLayout>(Resource.Id.atReasonLayout);          
        itemView.Click += (s, e) => listener(itemView);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下

if (vh.linearAbLayout.getVisibility() == View.VISIBLE)

     vh.linearAbLayout.seVisibility(VIEW.GONE);

 else
      vh.linearAbLayout.seVisibility(VIEW.VISIBLE);