繁体   English   中英

从片段中的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"
  ...>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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