2015-10-12 1 views
0

J'ai un scrollview dans une activité avec quelques fragments. L'un des fragments contient une vue d'image mobile, j'ai donc besoin d'arrêter le scrollview d'intercepter le panoramique.Désactivation d'un scrollview à partir d'un FrameLayout dans un fragment

J'ai un écouteur sur mon fragment pour définir un drapeau pour empêcher le scrollview de prendre l'interaction.

Le fragment TourMapFragment a les éléments suivants:

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

Et le fichier de mise en page:

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

Et dans TourMapRootLayout J'ai:

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

Alors, comment puis-je obtenir la référence au TourMapFragment à '??'

Répondre

0

Pour faire la mise en page au courant du fragment qui contient, vous pouvez créer un setter simple:

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

et de l'utiliser une fois que la vue a été gonflé (dans votre 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); 
} 

N'oubliez pas de définir l'ID sur votre réseau:

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