隐藏CardView时如何删除空间

时间:2017-07-18 03:26:21

标签: android layout cardview

隐藏CardView时如何删除空格?我用android和Firebase做了一个应用程序,我在cardviews中显示来自firebase的信息,但是当信息不正确时,cardview必须消失。关键是卡片视图消失了,但仍然在布局中使用了一个空格,我已经尝试了答案

setVisibility(GONE) view becomes invisible but still occupies space但它对我不起作用。

@Override
protected void onStart() {
    super.onStart();
    firebaseAuth.addAuthStateListener(firebaseAuthListener);

    final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
    new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
        CursosDB.class,
        R.layout.design_row_cursos,
        MainActivity.CursosPViewHolder.class,
        myRef)  
        {
            @Override
            protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position) 
            {
                String estado = Cmodel.getSTATUS().toString();
                if (estado.equals("OK")){
                    viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
                    viewHolder.setTitle(Cmodel.getTITLE());
                    viewHolder.setCiudad(Cmodel.getPLACE());
                    viewHolder.setLevel(Cmodel.getLEVEL());
                    viewHolder.setDur(Cmodel.getDURACION());
                    viewHolder.setPrice(Cmodel.getCOSTO());
                }else{
                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
                    layoutParams.setMargins(0,0,0,0);
                    viewHolder.mView.setLayoutParams(layoutParams);
                    viewHolder.mView.setVisibility(View.INVISIBLE);
                    viewHolder.mView.setVisibility(View.GONE);
                    //viewHolder.mView.setVisibility(View.GONE);
                    //Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
                }

                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
                        //Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
                        //Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
                    }
                });
            }
        };
    mCourses.setAdapter(firebaseRecyclerAdapter);
}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">

<LinearLayout android:id="@+id/layout_to_hide"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/Cimg"
        android:layout_width="match_parent"
        android:layout_height="165dp"
        android:scaleType="center"
        app:srcCompat="@drawable/knowit_logo" />

    <TextView
        android:id="@+id/Ctitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/Cimg"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:paddingTop="8dp"
        android:text="Titulo"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/Clugar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Ctitle"
        android:layout_alignStart="@+id/Ctitle"
        android:layout_below="@+id/Ctitle"
        android:layout_marginTop="10dp"
        android:text="Lugar" />

    <TextView
        android:id="@+id/textView29"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Clugar"
        android:layout_alignStart="@+id/Clugar"
        android:layout_below="@+id/Clugar"
        android:layout_marginTop="10dp"
        android:text="$" />

    <TextView
        android:id="@+id/Cprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView29"
        android:layout_marginBottom="4dp"
        android:layout_toEndOf="@+id/textView29"
        android:layout_toRightOf="@+id/textView29"
        android:paddingLeft="5dp"
        android:text="0.0" />

    <TextView
        android:id="@+id/Cdur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Clugar"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="126dp"
        android:layout_marginRight="126dp"
        android:text="Duración" />

    <TextView
        android:id="@+id/Cnivel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Cprice"
        android:layout_alignLeft="@+id/Cdur"
        android:layout_alignStart="@+id/Cdur"
        android:layout_marginBottom="0dp"
        android:text="Nivel" />

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

这是mView:

public static class CursosPViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public CursosPViewHolder(final View itemView) {
        super(itemView);
        mView = itemView;

    }

    public void setTitle(String title){
        TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
        post_title.setText(title);
    }

    public void setCiudad (String ciudad){
        TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
        post_city.setText(ciudad);
    }

    public void setPhotoURL(Context ctx, String pgotouserurl) {
        ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
        Picasso.with(ctx).load(pgotouserurl).into(post_image);
    }                   }

我希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

首先,您发布的XML代码错误关闭,(必须使用CardView标记完成) 此外,请确保您使用mView字段指向CardView布局ans仅使用GONE或INVISIBLE, 无法更改Panel(根视图)结构,但GONE不会