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
728x90
반응형
'Kotlin' 카테고리의 다른 글
코틀린 Tedpermission 사용하기 (0) | 2018.11.12 |
---|---|
(스크랩) 안드로이드 뷰페이저, 프레그먼트 페이지 관련 이슈 (0) | 2018.11.07 |
코틀린 뷰페이저 + 탭레이아웃 사용하기 (0) | 2018.10.30 |
코틀린 문자열 구분자를 가지고 나누어 리스트에 담기 (0) | 2018.10.26 |
코틀린 엑티비티를 다이얼로그로 띄우기 (0) | 2018.10.24 |