hide / show recyclerview:如何在显示时重置布局参数

时间:2018-03-05 13:38:22

标签: android android-recyclerview

在Recylerview中:我试图隐藏并显示视图

隐藏我正在做的事情:

     vh0.itemView.setVisibility(View.GONE);
     vh0.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

但是当我必须展示我正在尝试

    vh0.itemView.setVisibility(View.VISIBLE);                    
    RecyclerView.LayoutParams lpr = new 
    RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lpr.setMargins(10,10,10,10);
    vh0.itemView.setLayoutParams(lpr);

它不会将保存方式显示为以下的cardview。

我的cardview是

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cvsearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="#ffffff"
    card_view:cardCornerRadius="0dp"
    card_view:cardUseCompatPadding="false"
    card_view:contentPadding="0dp">

    <Button
        android:id="@+id/buttonsuggest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:clickable="false"
        android:layout_marginRight="20dp"
        android:text="Other Suggested Words" />


</android.support.v7.widget.CardView>

已解决:错误我在做: 我没有添加:

    RecyclerView.LayoutParams lpr = new 
    RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lpr.setMargins(10,10,10,10);
    vh0.itemView.setLayoutParams(lpr);

我只是在尝试:

vh0.itemView.setVisibility(View.VISIBLE);

以上是行不通的,因为之前我做过

vh0.itemView.setVisibility(View.GONE);
vh0.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

因此,即使我没有显示可见的边缘,也会删除边距。

0 个答案:

没有答案