0

当我尝试平移谷歌地图时,CoordinatorLayout 向下滚动,当然我想平移地图。如果我在地图之外然后尝试平移,那么它可以完美地工作,但这不会是一个很好的最终用户体验。

OnTouchListener 解决了部分问题……但前提是我水平平移。当我的手指向上或向下移动时,它就会停止平移地图。我怎样才能解决这个问题?

public class HomeFragment extends Fragment {

private TextView aboutTxt;
private SupportMapFragment mSupportMapFragment;
private GoogleMap mMap;
public HomeFragment() {
    // Required empty public constructor
}


// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance() {
    HomeFragment fragment = new HomeFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {

    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);


           view.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {



            if (event.getAction()==MotionEvent.ACTION_DOWN) {

                Log.e(TAG,"Down");
                return true;
            }

            if (event.getAction()==MotionEvent.ACTION_MOVE){

                Log.e(TAG,"Move");
                return true;

            }
            if (event.getAction()==MotionEvent.ACTION_UP){

                Log.e(TAG,"Up");
                return true;
            }


            return false;
        }
           });



    mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    if (mSupportMapFragment == null) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mSupportMapFragment = SupportMapFragment.newInstance();
        fragmentTransaction.replace(R.id.map, mSupportMapFragment).commit();
    }

    if (mSupportMapFragment != null) {
        mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                if (googleMap != null) {

                    mMap = googleMap;
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                    // Add a marker in Sydney and move the camera
                    LatLng myMap = new LatLng(xx.xxxx, xx.xxxx);
                    mMap.addMarker(new MarkerOptions().position(gradiMap).title("My marker));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myMap, 17));

                }


            }
        });
    }

XML

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <FrameLayout
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax" />


        <android.support.v7.widget.Toolbar
            android:id="@+id/main.toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/aboutTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="8dp"
        android:padding="@dimen/activity_horizontal_margin"
        android:textColor="@color/colorBlack"
        android:textSize="17sp" />

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:src="@drawable/whitestar"
    app:layout_anchor="@id/main.appbar"
    app:layout_anchorGravity="bottom|right|end" />

4

2 回答 2

2

我想你正在寻找OnInterceptTouchEvent. 您可以在您的设备上设置它,CoordinatorLayout并能够确定它是否应该使用事件本身,或者将其传递给它的子视图。

您需要扩展CoordinatorLayout和覆盖onInterceptTouchEvent(MotionEvent event)并执行以下操作:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            return false;
            break;
        case MotionEvent.ACTION_MOVE:
            return false;
            break;
        default:
            return true;
            break;
    }
}
于 2016-11-21T18:53:52.790 回答
0

在这里找到了答案 就像Teimoor Alam Khan所说,只需创建一个自定义类并使用它。

public class CustomScrollView extends ScrollView {

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

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

public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
    case MotionEvent.ACTION_DOWN:
        //Log.i("CustomScrollView", "onInterceptTouchEvent: DOWN super false" );
        super.onTouchEvent(ev);
        break;

    case MotionEvent.ACTION_MOVE:
        return false; // redirect MotionEvents to ourself

    case MotionEvent.ACTION_CANCEL:
        // Log.i("CustomScrollView", "onInterceptTouchEvent: CANCEL super false" );
        super.onTouchEvent(ev);
        break;

    case MotionEvent.ACTION_UP:
        //Log.i("CustomScrollView", "onInterceptTouchEvent: UP super false" );
        return false;

    default:
        //Log.i("CustomScrollView", "onInterceptTouchEvent: " + action );
        break;
}

return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
//Log.i("CustomScrollView", "onTouchEvent. action: " + ev.getAction() );
return true;
  }
}

XML:

<com.app.ui.views.CustomScrollView
android:id="@+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
</com.app.ui.views.CustomScrollView>
于 2016-11-22T14:05:59.943 回答