ZariApps
08/30/2020, 6:55 PMclass ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
itemView.setOnClickListener {
val intent = Intent(itemView.context, ListFragment::class.java)
itemView.context.startActivity(intent)
}
}
}
why is it giving me an error message saying i need to declare fragment into manifest file? is it because of the intent?Adam Powell
08/30/2020, 6:59 PMZariApps
08/30/2020, 6:59 PMAdam Powell
08/30/2020, 7:01 PMAdam Powell
08/30/2020, 7:02 PMZariApps
08/30/2020, 7:02 PMZariApps
08/30/2020, 7:02 PMAlejandro Rios
08/30/2020, 7:11 PMAlejandro Rios
08/30/2020, 7:11 PMZariApps
08/30/2020, 7:11 PMAlejandro Rios
08/30/2020, 7:13 PMcontext
to the adapter, the article is doing that, but IMHO is bad practiceZariApps
08/30/2020, 7:26 PMinterface CellClickListener {
fun onCellClickListener(data: HomeFeed)
}
ZariApps
08/30/2020, 7:26 PMZariApps
08/30/2020, 7:29 PMclass ListFragment : Fragment() {
interface CellClickListener {
fun onCellClickListener(data: HomeFeed)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view: View = inflater.inflate(R.layout.fragment_list, container, false)
return view
}
}
ZariApps
08/30/2020, 7:29 PMAlejandro Rios
08/30/2020, 7:31 PMclass ListFragment : Fragment(), CellClickListener
ZariApps
08/30/2020, 7:31 PMZariApps
08/30/2020, 7:35 PMZariApps
08/30/2020, 7:35 PMZariApps
08/30/2020, 7:36 PMoverride fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = homeFeed.data[position]
holder.itemView.counter_image.text = currentItem.bookNumber.toString()
holder.itemView.title.text = currentItem.book[0].name
holder.itemView.disc.text = currentItem.book[1].name
holder.itemView.setOnClickListener {
cellClickListener.onCellClickListener(currentItem)
}
}
ZariApps
08/30/2020, 7:38 PMAlejandro Rios
08/30/2020, 7:39 PMZariApps
08/30/2020, 7:40 PMclass RecyclerAdapter(val homeFeed: HomeFeed) :
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.recyclerlist, parent, false
)
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = homeFeed.data[position]
holder.itemView.counter_image.text = currentItem.bookNumber.toString()
holder.itemView.title.text = currentItem.book[0].name
holder.itemView.disc.text = currentItem.book[1].name
holder.itemView.setOnClickListener {
cellClickListener.onCellClickListener(currentItem)
}
}
override fun getItemCount() = homeFeed.data.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
}
Alejandro Rios
08/30/2020, 7:41 PMZariApps
08/30/2020, 7:42 PMZariApps
08/30/2020, 7:42 PMZariApps
08/30/2020, 7:43 PMZariApps
08/30/2020, 7:43 PMAlejandro Rios
08/30/2020, 7:44 PMHomeFeed
and the CellClickListener
2. ViewHolder should be declared in another class, not in the same as the adapter.
3. ViewHolder can contain a function called bind
which receives one homeFeed item and you display it as you wishZariApps
08/30/2020, 7:47 PMZariApps
08/30/2020, 7:47 PMZariApps
08/30/2020, 7:52 PMAlejandro Rios
08/30/2020, 7:59 PMAlejandro Rios
08/30/2020, 8:00 PM