[Android] CardStackView 사용하기

728x90

🟦 CardStackView 사용하기

▶️ CardStackView 사용하기

https://github.com/yuyakaido/CardStackView
  • module 수준의 gradle 파일에서 의존 추가
dependencies {
    implementation "com.yuyakaido.android:card-stack-view:${LatestVersion}"
}

🟧 activity_main.xml에 cardStackView 영역 잡아놓기

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.yuyakaido.android.cardstackview.CardStackView
        android:id="@+id/cardStackView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

🟧 item_card.xml 생성

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_margin="20dp"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/profileImageArea"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:src="@drawable/no" />
        <TextView
            android:text="Name"
            android:layout_margin="5dp"
            android:textSize="30sp"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="Age"
            android:layout_margin="5dp"
            android:textSize="30sp"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="City"
            android:layout_margin="5dp"
            android:textSize="30sp"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/left_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:src="@drawable/no"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"/>

    </FrameLayout>

    <FrameLayout
        android:id="@+id/right_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:src="@drawable/ok"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"/>

    </FrameLayout>

</androidx.cardview.widget.CardView>

🟧 CardStackAdapter 생성

class CardStackAdapter(val context : Context, val items : List<String>) : RecyclerView.Adapter<CardStackAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardStackAdapter.ViewHolder {

        val inflater = LayoutInflater.from(parent.context)
        val view: View = inflater.inflate(R.layout.item_card, parent, false)
        return ViewHolder(view)

    }

    override fun onBindViewHolder(holder: CardStackAdapter.ViewHolder, position: Int) {
        holder.binding(items[position])
    }

    override fun getItemCount(): Int {
        return items.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun binding(data: String) {

        }

    }
}

🟧 MainActivity.kt 에서 Adapter 연결

class MainActivity : AppCompatActivity() {

    lateinit var cardStackAdapter : CardStackAdapter
    lateinit var manager : CardStackLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val cardStackView = findViewById<CardStackView>(R.id.cardStackView)

        manager = CardStackLayoutManager(baseContext, object : CardStackListener {
            override fun onCardDragging(direction: Direction?, ratio: Float) {

            }

            override fun onCardSwiped(direction: Direction?) {

            }

            override fun onCardRewound() {

            }

            override fun onCardCanceled() {

            }

            override fun onCardAppeared(view: View?, position: Int) {

            }

            override fun onCardDisappeared(view: View?, position: Int) {

            }

        })

        val testList = mutableListOf<String>()
        testList.add("a")
        testList.add("b")
        testList.add("c")

        cardStackAdapter = CardStackAdapter(baseContext, testList)
        cardStackView.layoutManager = manager
        cardStackView.adapter = cardStackAdapter

    }
}

728x90