Tuesday, June 12, 2018

OnClick event isn't being triggered in fragment inside ViewPager

Leave a Comment

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;             }         }     } 

3 Answers

Answers 1

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 }  

Answers 2

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

Answers 3

How did you get that includedLayout method from Binding class?
I can't find it anywhere and that was the mistake I got, instead you should just use mBind.setHandlers(presenter);
At first I got error for Binding.setHandler method but after build or running it got solved and worked as expected.
And
Your MyPresenter class should have the exact method signature. for handlers.onLayoutClick() it should be:

public void onLayoutClick(){ ....} 

There isn't any problem with ViewPager, I tried exactly as yours.
Also check mBind's Type, from your code I guess It should be FragmentLayoutBinding.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment