sunflower icon indicating copy to clipboard operation
sunflower copied to clipboard

Proposal to remove clicklistener logic from adapter

Open andhie opened this issue 7 years ago • 2 comments

  1. define an interface OnPlantClickedListener(View view, Plant plant). the view can be used for transition animation later.
  2. implement in Fragment with the Navigation logic
  3. In the XML of the list item, at the root will be android:onClick="@{v -> listener.onPlantClicked(v, plant)}"
  4. When creating the Adapter, pass in the OnPlantClickedListener or create setOnPlantClickedListener method in adapter

andhie avatar Jul 17 '18 15:07 andhie

This is the approach I'd love to have. I'm having such a hard time implementing an onclicklistener to navigate to a "detail fragment". I don't want to use the Navigation component yet.

Could you give some more info? I'm new to android development and it's been tough for me to adapt to this new environment.

So, what I've understood is that I have to define an interface inside the adapter interface iOnClickListener{ fun OnPlantClickedListener(view: View, plant: Plant) }

Then I implement that interface in the Fragment

override fun OnPlantClickedListener(view: View, plant: Plant) { //navigation stuff }

Points 3 and 4 I cannot understand very well.

I mean, in the list item XML, do I have to specify a variable just like the clickListener or the Plant? Wich one would it be?

Also, I don't get how to pass the OnPlantClickListener or how to create the setOnPlantClickedListener.

And I wonder what would I have to do with the clickListener function that is already in the adapter.

I will totally understand if you don't want to help me with this, I've been googling for days and although I have found some info on this subject I am not abe to implement it in "the sunflower project·.

Darius82 avatar Aug 29 '18 13:08 Darius82

@Darius82 I believe it's late answer for you but still. Point 3 is Databinding component. This is already implemented in list_item_plant.xml, we just change interface from VIew.OnClickListener with created by us OnPlantClickedListener, and then we change "onClick" in XML to call specified method. Point 4 it when, in Fragment's onCreateView when we create PlantAdapter() we provide him either into constructor or setMethod instance of this OnPlantClickedListener (for simplify lets say its fragment itself "this"). PlanAdapter saves this object as a member variable and then, in ViewHolder.bind() method, we pass this member variable to xml binding.

KoTius avatar Nov 05 '18 08:11 KoTius