0

Hi i am new to android i was trying to make the swipe to dismiss feature with cardview & Recyclerview using itemtouchhelper.callback so i need to pass cardview as the only item in RecyclerView as this answer mentioned but i don't know how to extend the cardView to make customview?

cardStack

import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.LayoutInflater;


public class CardStack extends CardView {
  private int image;


    public CardStack(Context context) {
        super(context);
        initialize(context);
    }

    public CardStack(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public CardStack(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initialize(Context context){
        LayoutInflater.from(context).inflate(R.layout.card,this);
        //What to do here?
    }

card.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.stocks.android.cardviewobjectswipefeaturestocks.CardStack
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/card1" />

    </com.stocks.android.cardviewobjectswipefeaturestocks.CardStack>

</LinearLayout>
Community
  • 1
  • 1
Misterrai
  • 77
  • 1
  • 9

1 Answers1

0

Easy.

Make a layout file, with parent as CardView. I`ve made a row_list_item.xml which looks like this

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:fillViewport="true"
    app:cardBackgroundColor="@color/OFF_WHITE"
    app:cardCornerRadius="4dp"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="false">
<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
.........
</LinearLayout>
</CardView>

Then inside your adapter pass it to the adapter this way:

  @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.row_list_item, parent, false));
    }

And you're done. Kudos!

MadScientist
  • 2,134
  • 14
  • 27