Reputation: 2184
thank you for reading my question,
I've Implemented onClick in recyclerView using dataBinding but I can send the object itself to click function but I need the item position is it possible to get item position in onClick
?
here is how click is implemented in the recyclerView item:
android:onClick="@{ (v) -> model.click(obj) }"
Upvotes: 1
Views: 1630
Reputation: 18202
Create Interface
interface ItemClickListener{
void itemClicked(int position)
}
Use ItemClickListener and Integer in XML and call interface method on onClick()
like this
android:onClick="@{()->itemClickListener.itemClicked(position)}"
Demo XML snippet my_item.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="itemClickListener"
type="ItemClickListener" />
<variable
name="position"
type="Integer"/>
</data>
<LinerLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/offer_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{()->itemClickListener.itemClicked(position)}"
/>
</LinearLayout>
</layout>
Set ItemClickListener and Position in Adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ItemClickListener itemClickListener;
public MyAdapter(Context){
itemClickListener = (ItemClickListener)context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.my_item, parent, false);
return new MyViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull OfferViewHolder holder, int position) {
holder.bind(position);
}
class MyViewHolder extends RecyclerView.ViewHolder{
private MyItemBinding binding;
public OfferViewHolder(@NonNull ItemOfferBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
private void bind(int position){
binding.setItemClickListener(itemClickListener); //<----This is important
binding.setPosition(position); //<-- This is important
}
}
}
Activity Code
MyActicity extends AppCompatActivity{
MyAdapter adapter = new Adapter(this);
}
Upvotes: 4
Reputation: 2301
You can use adapterPosition
in view holder to pass item position to ItemView and then you can pass itemPosition
to android:onClick="@{ (v) -> model.click(obj, itemPosition) }"
class GeneralHolder<DataModel, ClickListner>(
private val binding: ViewDataBinding,
val listener: ClickListner
) :
RecyclerView.ViewHolder(binding.root) {
fun bind(data: DataModel) {
binding.setVariable(BR.item, data)
binding.setVariable(BR.presenter, listener)
binding.setVariable(BR.itemPosition, adapterPosition)
binding.executePendingBindings()
}
}
Upvotes: 0
Reputation: 573
There is an exposed property that you can use inside ViewHolder, when using databinding.
Just call adapterPosition
.
Upvotes: 0
Reputation: 210
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder.hTextview_Score.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
make click of your view in OnBindViewHolder and position is position of view
Upvotes: 0