从片段中的 FrameLayout 禁用滚动视图

Disabling a scrollview from an FrameLayout in a fragment

我在 Activity 中有一个带有一些片段的滚动视图。 其中一个片段包含一个可移动的图像视图,因此我需要阻止滚动视图拦截平移。

我的片段上有一个监听器来设置一个标志以防止滚动视图进行交互。

TourMapFragment 片段具有以下内容:

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

和布局文件:

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

在 TourMapRootLayout 中我有:

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

那么如何在“??”获取对 TourMapFragment 的引用

为了让布局知道包含它的片段,您可以创建一个简单的 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);
}

并在视图膨胀后使用它(在您的片段中):

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

记得在布局上设置 id:

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