Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

add horizontalDrag attr to disable or enable the layer horizontal drag #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,6 +167,8 @@ public float getInterpolation(float t) {

private LayerTransformer mLayerTransformer;

private boolean horizontalDrag;

public SlidingLayer(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Library/src/main/res/values/slidinglayer-attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<enum name="top" value="-3"/>
<enum name="bottom" value="-4"/>
</attr>
<attr name="horizontalDrag" format="boolean"/>
</declare-styleable>

</resources>
Original file line number Diff line number Diff line change
@@ -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<Fragment> 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<Fragment> fragmentList;

public TestFragmentAdapter(FragmentManager fm, List<Fragment> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.wunderlist.slidinglayer.SlidingLayer
xmlns:slidingLayer="http://schemas.android.com/apk/res-auto"
android:id="@+id/slidingLayer"
android:layout_width="match_parent"
android:layout_height="300dp"
slidingLayer:offsetDistance="95dp"
slidingLayer:previewOffsetDistance="@dimen/preview_offset_distance"
slidingLayer:stickTo="bottom"
slidingLayer:changeStateOnTap="true"
slidingLayer:horizontalDrag="false"
android:layout_gravity="bottom">

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</com.wunderlist.slidinglayer.SlidingLayer>

</FrameLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark">

<TextView
android:id="@+id/tvGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Go Go Go"
android:textSize="18sp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal"/>

</LinearLayout>