Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClick event isn't being triggered in fragment inside ViewPager

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;
            }
        }
    }
like image 913
matip Avatar asked May 21 '18 14:05

matip


2 Answers

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.

like image 61
Bertram Gilfoyle Avatar answered Oct 23 '22 18:10

Bertram Gilfoyle


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

like image 30
Aishik kirtaniya Avatar answered Oct 23 '22 19:10

Aishik kirtaniya