简体   繁体   中英

Disabling a scrollview from an FrameLayout in a fragment

I have a scrollview in an Activity with a few fragments. One of the fragments contains an movable image view, so I need to stop the scrollview from intercepting the pan.

I have a listener on my fragment to set a flag to prevent the scrollview from taking the interaction.

The TourMapFragment fragment has the following:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    void selectedTourPoint(int position);
    void setScrollTouchInteraction(boolean enabled);
}

public void setScrollTouchInteraction(boolean enabled){
    mListener.setScrollTouchInteraction(enabled);
}

And the layout file:

<TourMapRootLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.idoms.iDomsAndroid.fragments.TourMapFragment"
    android:background="@color/mapViewBackground">

    <org.idoms.iDomsAndroid.views.ResizableImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tourMap_imageView"
        android:layout_gravity="center_horizontal"
        android:scaleType="matrix" />

</TourMapRootLayout>

And in TourMapRootLayout I have:

public class TourMapRootLayout extends FrameLayout {

    public TourMapRootLayout(Context context) {
        super(context);
    }

    public TourMapRootLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TourMapRootLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ((TourMapFragment) getContext().getSupportFragmentManager().findFragmentById(R.id.FragmentContainer));
                ??.setScrollTouchInteraction(false);
                break;
            case MotionEvent.ACTION_UP:
                ??.setScrollTouchInteraction(true);
                break;
        }
        return false;

        return super.onInterceptTouchEvent(ev);
    }
}

So how do I get the reference to the TourMapFragment at '??'

To make the layout aware of the fragment that contains it, you can create a simple setter:

private TourMapFragment mFragment;

public void setFragment(TourMapFragment fragment) {
  mFragment = fragment;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
  // Default action if the fragment isn't set
  if (mFragment == null) super.onInterceptTouchEvent(ev);

  switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
      mFragment.setScrollTouchInteraction(false);
      break;
    case MotionEvent.ACTION_UP:
      mFragment.setScrollTouchInteraction(true);
      break;
  }
  return false;

  return super.onInterceptTouchEvent(ev);
}

And use it once the view has been inflated (in your fragment):

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  TourMapRootLayout layout = (TourMapRootLayout) view.findViewById(R.id.tour_map_root_layout);
  layout.setFragment(this);
}

Remember to set the id on your layout:

<TourMapRootLayout
  ...
  android:id="@+id/tour_map_root_layout"
  ...>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM