From acc36f1a94c15e9c3f711325841c4e3962f7a73a Mon Sep 17 00:00:00 2001 From: tubingbing Date: Thu, 17 Dec 2015 15:09:17 +0800 Subject: [PATCH] add horizontalDrag attr to disable or enable the layer horizontal drag --- .../wunderlist/slidinglayer/SlidingLayer.java | 10 ++- .../main/res/values/slidinglayer-attrs.xml | 1 + .../IncludeViewPagerActivity.java | 85 +++++++++++++++++++ .../res/layout/activity_include_viewpager.xml | 26 ++++++ .../res/layout/fragment_include_viewpager.xml | 17 ++++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 SlidingLayerSample/src/main/java/com/slidinglayersample/IncludeViewPagerActivity.java create mode 100644 SlidingLayerSample/src/main/res/layout/activity_include_viewpager.xml create mode 100644 SlidingLayerSample/src/main/res/layout/fragment_include_viewpager.xml diff --git a/Library/src/main/java/com/wunderlist/slidinglayer/SlidingLayer.java b/Library/src/main/java/com/wunderlist/slidinglayer/SlidingLayer.java index a044e59..e9ef70b 100644 --- a/Library/src/main/java/com/wunderlist/slidinglayer/SlidingLayer.java +++ b/Library/src/main/java/com/wunderlist/slidinglayer/SlidingLayer.java @@ -38,6 +38,7 @@ import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewConfigurationCompat; import android.util.AttributeSet; +import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.VelocityTracker; @@ -166,6 +167,8 @@ public float getInterpolation(float t) { private LayerTransformer mLayerTransformer; + private boolean horizontalDrag; + public SlidingLayer(Context context) { this(context, null); } @@ -216,6 +219,9 @@ public SlidingLayer(Context context, AttributeSet attrs, int defStyle) { mPreviewOffsetDistance = ta.getDimensionPixelOffset(R.styleable.SlidingLayer_previewOffsetDistance, INVALID_VALUE); + // Sets the ability to disable or enable the layer horizontal drag + horizontalDrag = ta.getBoolean(R.styleable.SlidingLayer_horizontalDrag, true); + // If showing offset is greater than preview mode offset dimension, exception is thrown checkPreviewModeConsistency(); @@ -587,7 +593,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { return false; } - final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff; + final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff && horizontalDrag; final boolean validVerticalDrag = yDiff > mTouchSlop && yDiff > xDiff; if (validHorizontalDrag) { @@ -686,7 +692,7 @@ public boolean onTouchEvent(MotionEvent ev) { final float xDiff = Math.abs(x - mInitialRawX); final float yDiff = Math.abs(y - mInitialRawY); - final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff; + final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff && horizontalDrag; final boolean validVerticalDrag = yDiff > mTouchSlop && yDiff > xDiff; if (validHorizontalDrag || validVerticalDrag) { diff --git a/Library/src/main/res/values/slidinglayer-attrs.xml b/Library/src/main/res/values/slidinglayer-attrs.xml index b595f34..7f090a7 100644 --- a/Library/src/main/res/values/slidinglayer-attrs.xml +++ b/Library/src/main/res/values/slidinglayer-attrs.xml @@ -36,6 +36,7 @@ + \ No newline at end of file diff --git a/SlidingLayerSample/src/main/java/com/slidinglayersample/IncludeViewPagerActivity.java b/SlidingLayerSample/src/main/java/com/slidinglayersample/IncludeViewPagerActivity.java new file mode 100644 index 0000000..c452dbb --- /dev/null +++ b/SlidingLayerSample/src/main/java/com/slidinglayersample/IncludeViewPagerActivity.java @@ -0,0 +1,85 @@ +package com.slidinglayersample; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.support.v7.app.AppCompatActivity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import com.wunderlist.slidinglayer.SlidingLayer; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by tubingbing on 15/12/17. + */ +public class IncludeViewPagerActivity extends AppCompatActivity{ + + ViewPager vp; + SlidingLayer slidingLayer; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_include_viewpager); + vp = (ViewPager) findViewById(R.id.vp); + slidingLayer = (SlidingLayer) findViewById(R.id.slidingLayer); + + List fragmentList = new ArrayList<>(10); + for (int i=0; i< 10; i++){ + Fragment fragment = TestFragment.newInstance(); + fragmentList.add(fragment); + } + + TestFragmentAdapter adapter = new TestFragmentAdapter(getSupportFragmentManager(), fragmentList); + vp.setAdapter(adapter); + } + + public static class TestFragmentAdapter extends FragmentPagerAdapter{ + + private List fragmentList; + + public TestFragmentAdapter(FragmentManager fm, List fragmentList) { + super(fm); + this.fragmentList = fragmentList; + } + + @Override + public Fragment getItem(int position) { + return fragmentList.get(position); + } + + @Override + public int getCount() { + return fragmentList.size(); + } + } + + public static class TestFragment extends Fragment{ + + public static Fragment newInstance(){ + return new TestFragment(); + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_include_viewpager, container, false); + View tvGo = view.findViewById(R.id.tvGo); + tvGo.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(getActivity(), "GO GO GO", Toast.LENGTH_SHORT).show(); + } + }); + return view; + } + } +} diff --git a/SlidingLayerSample/src/main/res/layout/activity_include_viewpager.xml b/SlidingLayerSample/src/main/res/layout/activity_include_viewpager.xml new file mode 100644 index 0000000..7344ea3 --- /dev/null +++ b/SlidingLayerSample/src/main/res/layout/activity_include_viewpager.xml @@ -0,0 +1,26 @@ + + + + + + + + + + \ No newline at end of file diff --git a/SlidingLayerSample/src/main/res/layout/fragment_include_viewpager.xml b/SlidingLayerSample/src/main/res/layout/fragment_include_viewpager.xml new file mode 100644 index 0000000..c19c5eb --- /dev/null +++ b/SlidingLayerSample/src/main/res/layout/fragment_include_viewpager.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file