Search code examples
androidonclicklistenerandroid-xmlontouchlistenerandroid-touch-event

can i access resources or views of fragment inside its baseactivity


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!


Solution

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