Kotlin

코틀린 리싸이클러뷰 recyclerView 사용하기

Machine_웅 2018. 11. 2. 14:58
728x90
반응형

일반적으로 사용하기

 

<<메인>>


class MainActivity : AppCompatActivity() {

    val animals: ArrayList<String> = ArrayList()



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

        addAnimals()
        test.layoutManager = LinearLayoutManager(this)
        test.adapter = testAdapter(animals, this)

    }

    fun addAnimals() {
        animals.add("dog")
        animals.add("cat")
        animals.add("owl")
        animals.add("cheetah")
        animals.add("raccoon")
        animals.add("bird")
        animals.add("snake")
        animals.add("lizard")
        animals.add("hamster")
        animals.add("bear")
        animals.add("lion")
        animals.add("tiger")
        animals.add("horse")
        animals.add("frog")
        animals.add("fish")
        animals.add("shark")
        animals.add("turtle")
        animals.add("elephant")
        animals.add("cow")
        animals.add("beaver")
        animals.add("bison")
        animals.add("porcupine")
        animals.add("rat")
        animals.add("mouse")
        animals.add("goose")
        animals.add("deer")
        animals.add("fox")
        animals.add("moose")
        animals.add("buffalo")
        animals.add("monkey")
        animals.add("penguin")
        animals.add("parrot")
    }

}

 

<<어댑터>>

 


class testAdapter (val items : ArrayList<String>, val context: Context) : RecyclerView.Adapter<ViewHolder>(){


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false))

    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder?.tvAnimalType?.text = items.get(position)
    }


}

class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
    // Holds the TextView that will add each animal to
    val tvAnimalType = view.testTV
}

 

 

 

 

 

 

 

 

 

 

 

프레그먼트에서 사용 하기

 

 

<<메인>>

 


class Friend_ListPage : Fragment() {

    val friend_DataArray : ArrayList<friend_data> = ArrayList()
    lateinit var recyclerView1 : RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment


        var rootView = inflater.inflate(R.layout.fragment_friend__list_page, container, false)
        friend_DataArray.add(friend_data("이름","닉","이메일","사진"))

        recyclerView1 = rootView.findViewById(R.id.friend_List_RV!!)as RecyclerView
        recyclerView1.layoutManager = LinearLayoutManager(requireContext())
        recyclerView1.adapter = friend_List_Adapter(requireContext(),friend_DataArray)

        return rootView
    }


}

 

recyclerView1 = rootView.findViewById(R.id.friend_List_RV!!)as recyclerView
recyclerView1.layoutManager = LinearLayoutManager(requireContext())
recyclerView1.adapter = friend_List_Adapter(requireContext(),friend_DataArray)

 

 

 

<<어댑터>>


class friend_List_Adapter  (val context: Context, val friend_DataArray: ArrayList<friend_data>) : RecyclerView.Adapter<mViewH>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): mViewH {

      /*  val view = LayoutInflater.from(context).inflate(R.layout.friend_list_item, parent, false)
        return mViewH(view)*/

        return mViewH(LayoutInflater.from(context).inflate(R.layout.friend_list_item,parent,false))
    }

    override fun getItemCount(): Int {

        // return friend_DataArray.size
        return 10
    }

    override fun onBindViewHolder(holder: mViewH, position: Int) {

    /*    // 닉네임없음 프로필없음 배경없음 번호없음
        holder?.friend_Name.text = "하이"*/

        Log.d("리사이클러뷰 가 불러짐","ㅇㅇㅇㅇ")


    }

}

class mViewH(view: View) : RecyclerView.ViewHolder(view!!) {
    var friend_Profile = view.friend_List_circle_Profile
    var friend_Name = view.friend_List_friendName
    var friend_Status = view.friend_List_friendStatus


}

https://kadosholy.tistory.com/55

 

[안드로이드] RecyclerView 업데이트 및 갱신 방법

RecyclerView 업데이트 및 갱신 방법 RecyclerView의 아이템 내용이 변경되거나 아이템이 추가/이동/삭제 되었을 경우 RecyclerView에 반영하는 방법에 대해서 알아보고자 합니다. RecyclerView를 업데이트 및

kadosholy.tistory.com

 

728x90
반응형