I have my fragments inside ViewPager
and I would like in one of them to have clickable RelativeLayout
. I am using data binding:
<data>
<variable
name="handlers"
type="com.matip.presenters.MyPresenter" />
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> handlers.onLayoutClick()}">
...
...
...
</RelativeLayout>
My Fragments onCreateView:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_layout, container, false);
presenter = new MyPresenter();
mBind.includedLayout.setHandlers(presenter);
return mBind.getRoot();
}
But the onLayoutClick
is not being called. Does it have something to do with ViewPager? Does it take the click and not the fragment inside it? If so how can I fix that?
EDIT
My ViewPager Adapter:
private class MyViewPagerAdapter extends FragmentStatePagerAdapter {
public MyViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return MainInfoFragment.newInstance(parkingId);
case 1:
return MainDescFragment.newInstance(parkingId);
case 2:
return MainServicesFragment.newInstance(parkingId);
default:
return null;
}
}
@Override
public int getCount(){
return 3;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch(position){
case 0: return getContext().getString(R.string.info_title);
case 1: return getContext().getString(R.string.desc_title);
case 2: return getContext().getString(R.service_title);
default: return null;
}
}
}
To get onClick
attribute in XML to be worked, the method need to be in activity. For fragments the preferred method is setting a listener programmatically. The XML method will work if the method is in the activity but it will become complicated if the method need to be behaved differently for different fragments.
Also I don't understand what you mean by android:onClick="@{() -> handlers.onLayoutClick()}"
. Normally it looks likeandroid:onClick="onLayoutClick"
. And the definition will be like
public void onLayoutClick(View view) {
//Your code
}
There is one more possibility: you might have set an OnTouchListener to the view which overrides the click. If you want both touch and click listeners, then try GestureDetector.
you may do the following on the oncreateview() method
oncreate(....)
{.....
View v = inflater.inflate(R.layout.fragment_layout,container,false);
RelativeLayout l = v.findViewById(R.id.rl);
l.setOnClickListener(this);
.....}
@Override
public void onClick(View v)
{
if(v.getId == R.id.rl)
{
//do your work
}
}
and change the layout to this
<RelativeLayout
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
...
...
...
</RelativeLayout>
press alt+enter on the clicklistener if the compiler shows error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With