从片段

时间:2015-10-12 11:43:06

标签: android android-fragments android-framelayout

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

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

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

那么如何在&#39; ??&#39;

中获得对TourMapFragment的引用

1 个答案:

答案 0 :(得分:0)

要使布局知道包含它的片段,您可以创建一个简单的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"
  ...>