728x90
반응형
<fragment>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater,R.layout.fragment_main_fragment, container, false)
binding.fragment = this
setAdapter()
return binding.root
}
// 리싸이클러뷰 연결
fun setAdapter() {
dataAdapter = BoardAdapter(dataArr, this, backImage)
binding.recyclerViewBoard.layoutManager =GridLayoutManager(context, 3, LinearLayoutManager.VERTICAL, false)
//recyclerView_Board.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
//recyclerView_Board.layoutManager = GridLayoutManager(context, 3, LinearLayoutManager.VERTICAL, false)
binding.recyclerViewBoard.adapter = dataAdapter
// recyclerView_Board.adapter = dataAdapter
}
예제에서는 liveData를 사용하지 않았으나, 사용하는것을 권합니다.
<Adapter>
1. onCreateViewHolder 에서 생성된 바인딩 객체를 초기화 하고, inflate해준다.
2.뷰홀더 에서 뷰를 바인드 해주고, 데이터를 넣어주자
class BoardAdapter (
val dataSet: MutableList<BoardData>,
val inter : BoardInter,
val backImgArr : ArrayList<Int>) :
RecyclerView.Adapter<BoardAdapter.DViewHolder>(){
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): BoardAdapter.DViewHolder {
// 기존
// val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.item_board, viewGroup, false)
// return ViewHolder(view)
val binding : ItemBoardBinding =
ItemBoardBinding.inflate(LayoutInflater.from(viewGroup.context),viewGroup,false)
return DViewHolder(binding)
}
override fun onBindViewHolder(holder: BoardAdapter.DViewHolder, position: Int) {
// 기존 사용방식
/* holder.item_txt_title.text = dataSet.get(position).BoardName
holder.item_layout.setBackgroundResource(backImgArr.get(position))
holder.item_layout.setOnClickListener {inter.click(dataSet.get(position))}
if(dataSet.get(position).BoardNew){
holder.item_newNoti.visibility = View.VISIBLE
}else{
holder.item_newNoti.visibility = View.GONE
}*/
// 데이터바인딩
holder.onBind(dataSet.get(position),backImgArr.get(position),inter)
}
override fun getItemCount(): Int {
return dataSet.size
}
//기존 뷰홀더
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val item_txt_title: TextView
val item_layout : LinearLayout
val item_newNoti: TextView
init {
// Define click listener for the ViewHolder's View.
item_txt_title = view.findViewById(R.id.item_txt_title)
item_layout = view.findViewById(R.id.item_layout)
item_newNoti = view.findViewById(R.id.item_newNoti)
}
}
// 데이터 바인딩 뷰홀더
class DViewHolder(val binding : ItemBoardBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(data : BoardData, imageRes : Int ,inter: BoardInter){
// 데이터 입력
binding.itemTxtTitle.text = data.BoardName
binding.itemLayout.setBackgroundResource(imageRes)
binding.itemLayout.setOnClickListener { inter.click(data) }
if(data.BoardNew){
binding.itemNewNoti.visibility = View.VISIBLE
}else{
binding.itemNewNoti.visibility = View.GONE
}
}
}
}
<item xml>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:background="@drawable/btn_r_gray"
android:layout_margin="10dp"
android:id="@+id/item_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:id="@+id/item_txt_title"
android:text="test"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="right"
android:id="@+id/item_newNoti"
android:text="test"
android:textColor="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
</layout>
728x90
반응형
'Android 공부 > Android AAC' 카테고리의 다른 글
MVVM : RecyclerView, LiveData (0) | 2022.03.28 |
---|---|
DATA_Binding : Fragment (0) | 2022.03.10 |
MVVM LiveData (1) (0) | 2021.02.22 |
Android AAC : LiveData (0) | 2021.01.07 |
Android ACC : Data Binding (0) | 2021.01.06 |