안드로이드 multiViewRecyclerView

푸른하늘·2022년 5월 24일
0
post-thumbnail

<작동 영상>

순서

🍎itme_view1, item_view2, item_view3 을 3개를 만든다.
🍎 activity_main.xml 에 RecyclerView를 삽입한다.
🍎 multi_type을 지정 / data class => MultiData 생성
🍎 MutiviewAdapter 생성
🍎MainActivity 적용

🍏 itemView 지정

itemView1💥

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingHorizontal="20dp"
    android:paddingVertical="10dp"
    android:background="@color/purple_500"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/img_rv_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@color/black"
        android:transitionName="image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/tv_rv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름"
        android:transitionName="name"
        android:textStyle="bold"
        android:layout_marginLeft="20dp"
        android:textSize="24sp"
        android:textColor="@color/white"
        app:layout_constraintLeft_toRightOf="@id/img_rv_photo"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_rv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="나이"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:textSize="18sp"
        android:textColor="@color/white"
        app:layout_constraintLeft_toRightOf="@id/img_rv_photo"
        app:layout_constraintTop_toBottomOf="@id/tv_rv_name"/>


</androidx.constraintlayout.widget.ConstraintLayout>

itemview2💥

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingHorizontal="20dp"
    android:paddingVertical="10dp"
    android:background="@color/purple_500"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/img_rv_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@color/black"
        android:transitionName="image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/tv_rv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름"
        android:transitionName="name"
        android:textStyle="bold"
        android:layout_marginRight="20dp"
        android:textSize="24sp"
        android:textColor="@color/white"
        app:layout_constraintRight_toLeftOf="@id/img_rv_photo"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_rv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="나이"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:textSize="18sp"
        android:textColor="@color/white"
        app:layout_constraintRight_toLeftOf="@id/img_rv_photo"
        app:layout_constraintTop_toBottomOf="@id/tv_rv_name"/>


</androidx.constraintlayout.widget.ConstraintLayout>

itemview3💥

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingHorizontal="20dp"
    android:paddingVertical="10dp"
    android:background="@color/purple_500"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/img_rv_photo"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:src="@color/black"
        android:transitionName="image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tv_rv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름"
        android:transitionName="name"
        android:textStyle="bold"
        android:textSize="24sp"
        android:layout_marginVertical="20dp"
        android:textColor="@color/white"
        app:layout_constraintTop_toBottomOf="@id/img_rv_photo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

🍏activity_xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rv_profile"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

🍏MutilMode.kt

const val multi_type1 = 1
const val multi_type2 = 2
const val multi_type3 = 3

🍏MutilData.kt

data class MultiData (
    val image : Int,
    val name : String,
    val age : Int,
    val type : Int
        )

🍏MultiviewAdapter

class MultiviewAdapter(private val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var datas = mutableListOf<MultiData>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : RecyclerView.ViewHolder {
        val view : View?
        return when(viewType) {
            multi_type1 -> {
                val binding = ItemViewBinding.inflate(LayoutInflater.from(parent.context),parent,false)
                return  MultiViewHolder1(binding)
            }
            multi_type2 -> {
                val binding = ItemView2Binding.inflate(LayoutInflater.from(parent.context),parent,false)
                return  MultiViewHolder2(binding)
            }
            else -> {
                val binding = ItemView3Binding.inflate(LayoutInflater.from(parent.context),parent,false)
                return  MultiViewHolder3(binding)
            }
        }
    }
    override fun getItemCount(): Int = datas.size

    override fun getItemViewType(position: Int): Int {
        return datas[position].type
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(datas[position].type) {
            multi_type1 -> {
                (holder as MultiViewHolder1).bind(datas[position])
                holder.setIsRecyclable(false)
            }
            multi_type2 -> {
                (holder as MultiViewHolder2).bind(datas[position])
                holder.setIsRecyclable(false)
            }
            else -> {
                (holder as MultiViewHolder3).bind(datas[position])
                holder.setIsRecyclable(false)
            }
        }
    }

    inner class MultiViewHolder1(private val binding:ItemViewBinding) : RecyclerView.ViewHolder(binding.root) {



        fun bind(item: MultiData) {
            binding.tvRvName.text = item.name
            binding.tvRvAge.text = item.age.toString()
            Glide.with(binding.root).load(item.image).into(binding.imgRvPhoto)

        }
    }
    inner class MultiViewHolder2(private val binding: ItemView2Binding) : RecyclerView.ViewHolder(binding.root) {


        fun bind(item: MultiData) {
            binding.tvRvName.text = item.name
            binding.tvRvAge.text = item.age.toString()
            Glide.with(binding.root).load(item.image).into(binding.imgRvPhoto)

        }
    }

    inner class MultiViewHolder3(private val binding: ItemView3Binding) : RecyclerView.ViewHolder(binding.root) {


        fun bind(item: MultiData) {
            binding.tvRvName.text = item.name
            Glide.with(binding.root).load(item.image).into(binding.imgRvPhoto)

        }
    }
}

🍏MainActivity

class MainActivity : AppCompatActivity() {
    lateinit var multiAdapter: MultiviewAdapter
    val datas = mutableListOf<MultiData>()
    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initRecycler()
    }

    private fun initRecycler() {
        multiAdapter = MultiviewAdapter(this)
        binding.rvProfile.apply {
            adapter = multiAdapter

        }
        datas.apply { //drawable 은 임의로 인터넷에서 사진가져와서 넣으면 됩니다.!!!
            add(MultiData(image = R.drawable.unnamed1, name = "mary", age = 24, multi_type3))
            add(MultiData(image = R.drawable.unamed2, name = "jenny", age = 26, multi_type2))
            add(MultiData(image = R.drawable.unnamed3, name = "jhon", age = 27, multi_type1))
            add(MultiData(image = R.drawable.unnamed4, name = "ruby", age = 21, multi_type2))
            add(MultiData(image = R.drawable.unnamed1, name = "yuna", age = 23, multi_type3))

            multiAdapter.datas = datas
            multiAdapter.notifyDataSetChanged()
        }

    }
}
profile
Developer-Android-CK

0개의 댓글