0

I have a base activity in which i have inflated 4 fragments. i have several methods which i have to use in all the fragments.but i don't want that. I want to implement that method inside my base activity so that i can reuse my method to all the fragments. I want to use onTouchListener event of every parent layout of fragments. i can achieve this via xml onClick method and can call this method in baseActivity, But i want to use onTouch event, that method does't avalilable in layout xml. So i simply want to access resource from my fragment layout in my base activity and implement TouchEvent in baseActivity.

Is it Possible.?? Any help is Appreciated..Thanks!

3 Answers3

0

You could use getView()

Approach 1

Before calling getView you must be sure that your fragment's onCreateView method was executed (for example, you could attach a OnPageChangeListener to your viewpager, and inside the onPageSelected, you attach the OnTouchListener)

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            fragments.get(position).getView().setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return false;
                }
            });
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

Approach 2

Create a base fragment with a setOnTouchListener method:

public class TouchableFragment extends Fragment {

    protected View.OnTouchListener mOnTouchListener;

    public void setOnTouchListener(View.OnTouchListener mOnTouchListener) {
        this.mOnTouchListener = mOnTouchListener;
    }
}

Inside your fragment's onCreateView method:

public class MyFragment extends TouchableFragment {
    public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle s) {

        mView = inflater.inflate(
                R.layout.a_layout
                container,
                false
        );

        if(this.mOnTouchListener != null){
            mView.setOnTouchListener(this.mOnTouchListener);
        }

        return mView;
    }
}

And inside your activity:

MyFragment myFragment = new MyFragment();
myFragment.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }
});
Pedro Cardoso
  • 411
  • 2
  • 8
  • Hey, can you check my answer again? I forgot to mention that you must call getView() after you make sure that getView() was called, also added a new approach to solve this problem, thanks – Pedro Cardoso Oct 22 '16 at 11:10
0

You can define your views as static members of your fragment. Then you can access them inside your activity. If you did that, you should make sure that your fragment is accessible when you are using its static views.

A. Badakhshan
  • 1,045
  • 7
  • 22
0

if you make a one class called constant class into that declare your method as public static and using that class you can access this method to any activity and fragment, you have to pass argument context of your activity into that method.

may be this is helpful to you.

Mahesh Keshvala
  • 1,349
  • 12
  • 19