Skip to content

Commit

Permalink
Merge pull request #3 from wdullaer/touch_intercept
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
wdullaer committed Dec 14, 2014
2 parents df8da77 + 6fe58a0 commit 8608441
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
15 changes: 13 additions & 2 deletions example/src/main/res/layout/row_bg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="?android:listPreferredItemHeight"
android:background="@android:color/transparent">
<TextView
android:id="@+id/text"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textAppearance="?android:textAppearanceListItem"
android:text="Dummy"/>
<Space android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="16dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="16dp"
android:text="Button" />

</LinearLayout>
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME=1.2.0
VERSION_CODE=4
VERSION_NAME=1.3.0
VERSION_CODE=5
GROUP=com.wdullaer

ANDROID_BUILD_MIN_SDK_VERSION=14
Expand Down
4 changes: 4 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ task jar(type: Jar) {
from fileTree(dir: 'build/intermediates/classes/release/')
}

dependencies {
compile 'com.android.support:support-annotations:21.0.2'
}

apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
8 changes: 1 addition & 7 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wdullaer.swipeactionadapter"
android:versionCode="4"
android:versionName="1.0.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
package="com.wdullaer.swipeactionadapter" >

<application />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.wdullaer.swipeactionadapter;

import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
Expand All @@ -37,22 +37,22 @@ public class SwipeActionAdapter extends DecoratorAdapter implements
private boolean mFadeOut = false;
private boolean mFixedBackgrounds = false;

protected SparseArray<Integer> mBackgroundResIds = new SparseArray<Integer>();
protected SparseIntArray mBackgroundResIds = new SparseIntArray();

public SwipeActionAdapter(BaseAdapter baseAdapter){
super(baseAdapter);
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent){
// TODO: add our custom background images here (provide some good defaults)
SwipeViewGroup output = (SwipeViewGroup)convertView;

if(output == null) {
output = new SwipeViewGroup(parent.getContext());
for (int i = 0; i < mBackgroundResIds.size(); i++) {
output.addBackground(View.inflate(parent.getContext(),mBackgroundResIds.valueAt(i), null),mBackgroundResIds.keyAt(i));
}
output.setSwipeTouchListener(mTouchListener);
}

output.setContentView(super.getView(position,output.getContentView(),output));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.wdullaer.swipeactionadapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

Expand All @@ -31,7 +33,8 @@ public class SwipeViewGroup extends FrameLayout {
private View contentView = null;

private int visibleView = SwipeDirections.DIRECTION_NEUTRAL;
private SparseArray<View> mBackgroundMap = new SparseArray<View>();
private SparseArray<View> mBackgroundMap = new SparseArray<>();
private OnTouchListener swipeTouchListener;

/**
* Standard android View constructor
Expand All @@ -49,7 +52,7 @@ public SwipeViewGroup(Context context) {
* @param attrs
*/
public SwipeViewGroup(Context context, AttributeSet attrs) {
super(context,attrs);
super(context, attrs);
}

/**
Expand All @@ -60,7 +63,7 @@ public SwipeViewGroup(Context context, AttributeSet attrs) {
* @param defStyle
*/
public SwipeViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context,attrs,defStyle);
super(context, attrs, defStyle);
}

/**
Expand Down Expand Up @@ -129,6 +132,18 @@ public void translateBackgrounds(){
}
}

/**
* Set a touch listener the SwipeViewGroup will watch: once the OnTouchListener is interested in
* events, the SwipeViewGroup will stop propagating touch events to its children
*
* @param swipeTouchListener The OnTouchListener to watch
* @return A reference to the layout so commands can be chained
*/
public SwipeViewGroup setSwipeTouchListener(OnTouchListener swipeTouchListener) {
this.swipeTouchListener = swipeTouchListener;
return this;
}

@Override
public Object getTag() {
if(contentView != null) return contentView.getTag();
Expand All @@ -150,4 +165,16 @@ public Object getTag(int key) {
public void setTag(int key, Object tag) {
if(contentView != null) contentView.setTag(key, tag);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Start tracking the touch when a child is processing it
return super.onInterceptTouchEvent(ev) || swipeTouchListener.onTouch(this, ev);
}

@Override
public boolean onTouchEvent(@NonNull MotionEvent ev) {
// Finish the swipe gesture: our parent will no longer do it if this function is called
return swipeTouchListener.onTouch(this, ev);
}
}

0 comments on commit 8608441

Please sign in to comment.