2

Recyclerview CardView remove default Spacing When setting setVisibility is GONE to particular item

Image One When Item setVisibility is INVISIBLE

When Item setVisibility is INVISIBLE

Image Two When Item setVisibility is GONE

When Item setVisibility is GONE

Required Output

Required final Output

 <?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:layout_width="match_parent"
        android:layout_height="126dp"
        android:layout_marginLeft="8.00dp"
        android:layout_marginRight="8.00dp"
        android:layout_marginTop="2.00dp"
        android:layout_marginBottom="2.00dp"
        android:background="@color/white"
        app:cardCornerRadius="2dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/cardview">

    <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Sarath Kumar
  • 1,922
  • 3
  • 19
  • 43

3 Answers3

1

Use setVisibility gone , use this and yes remove extra margin you have given

<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/cardview3"
    android:layout_width="match_parent"
    android:layout_height="126dp"
    android:layout_marginLeft="8.00dp"
    android:layout_marginRight="8.00dp"
    android:background="@color/white"
    app:cardCornerRadius="2dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true">

    <FrameLayout
        android:id="@+id/container3"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v7.widget.CardView>

example

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stackone);

        final TextView textView1 = (TextView) findViewById(R.id.text1);
        final TextView textView2 = (TextView) findViewById(R.id.text2);
        final TextView textView3 = (TextView) findViewById(R.id.text3);

        final CardView cardView1 = (CardView) findViewById(R.id.cardView1);
        final CardView cardView2 = (CardView) findViewById(R.id.cardView2);
        final CardView cardView3 = (CardView) findViewById(R.id.cardView3);

        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView2.setVisibility(View.GONE);   //visibility gone for cardview textview

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                );
                params.setMargins(10, -5, 10, 10);//set your margin here
                cardView3.setLayoutParams(params);

                cardView3.setCardElevation(5);
            }
        });
    }
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

Just set the Height of your hidden view to 0 i thing this will solve your problem

shubham goyal
  • 537
  • 3
  • 3
0

Use gone and visible types of viewHolder. Where gone viewholder has height="0dp".

BaseRecyclerViewAdapter.java

public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseRecyclerViewAdapter.ViewHolder> {

private final int TYPE_GONE = 0;
private final int TYPE_VISIBLE = 1;
private int gonePosition;

@Override
public int getItemViewType(int position) {
    return position == gonePosition ? TYPE_GONE : TYPE_VISIBLE;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    if (viewType == TYPE_VISIBLE){
        return new ViewHolder(inflater.inflate(R.layout.item_card_visible, parent, false));
    }else {
        return new ViewHolder(inflater.inflate(R.layout.item_card_gone, parent, false));
    }
}

public void setGonePosition(int gonePosition) {
    this.gonePosition = gonePosition;
}

item_card_visible.xml

 <android.support.v7.widget.CardView 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="200dp">

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

item_card_gone.xml

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="0dp">

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

usage

adapter.setGonePosition(...);
adapter.notifyDataSetChanged();

optimization

Remember last hidden position, and use notifyItemChanged

public void setGonePosition(int gonePosition) {
    final int oldGonePosition = this.gonePosition;
    this.gonePosition = gonePosition;

    if (oldGonePosition != -1){
        notifyItemChanged(oldGonePosition);
    }
    notifyItemChanged(gonePosition);
}
Vitalii Movchan
  • 630
  • 7
  • 4