Skip to content

Commit

Permalink
support double click
Browse files Browse the repository at this point in the history
  • Loading branch information
daimajia committed Sep 22, 2014
1 parent 83bc638 commit e0b54de
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.AdapterView;
import android.widget.GridView;

import com.daimajia.swipe.implments.SwipeItemMangerImpl;
import com.daimajia.swipedemo.adapter.GridViewAdapter;

public class GridViewExample extends Activity{
Expand All @@ -17,7 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.gridview);
final GridView gridView = (GridView)findViewById(R.id.gridview);
final GridViewAdapter adapter = new GridViewAdapter(this);

adapter.setMode(SwipeItemMangerImpl.Mode.Multiple);
gridView.setAdapter(adapter);
gridView.setSelected(false);
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.daimajia.swipedemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -11,6 +12,7 @@
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.daimajia.swipe.implments.SwipeItemMangerImpl;
import com.daimajia.swipedemo.adapter.ListViewAdapter;
Expand All @@ -19,7 +21,7 @@ public class ListViewExample extends Activity {

private ListView mListView;
private ListViewAdapter mAdapter;

private Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -41,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("ListView", "onItemClick:" + position);
Toast.makeText(mContext, "Click", Toast.LENGTH_SHORT).show();
}
});
mListView.setOnTouchListener(new View.OnTouchListener() {
Expand All @@ -54,7 +56,7 @@ public boolean onTouch(View v, MotionEvent event) {
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("ListView","onItemLongClick:" + position);
Toast.makeText(mContext, "OnItemLongClickListener", Toast.LENGTH_SHORT).show();
return false;
}
});
Expand Down
18 changes: 15 additions & 3 deletions demo/src/main/java/com/daimajia/swipedemo/NestedExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.daimajia.swipe.SwipeLayout;

public class NestedExample extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.complicate_layout);
findViewById(R.id.hhhhh).setOnClickListener(new View.OnClickListener() {
SwipeLayout swipeLayout = (SwipeLayout)findViewById(R.id.test_swipe_swipe);
swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
@Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(getApplicationContext(), "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
swipeLayout.findViewById(R.id.trash).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Tag","got");
Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show();
}
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.daimajia.swipedemo.R;

public class ListViewAdapter extends BaseSwipeAdapter {
Expand All @@ -36,6 +37,12 @@ public void onOpen(SwipeLayout layout) {
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
}
});
swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
@Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
return v;
}

Expand Down
2 changes: 2 additions & 0 deletions demo/src/main/res/layout/complicate_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
<include layout="@layout/sampe_nested_scrollview" android:layout_height="80dp" android:layout_width="match_parent"/>
<include layout="@layout/sampe_nested_seekbar" android:layout_height="80dp" android:layout_width="match_parent" android:layout_marginTop="20dp"/>
<include layout="@layout/sampe_nested_edittext" android:layout_height="80dp" android:layout_width="match_parent" android:layout_marginTop="20dp"/>
<include layout="@layout/sample_nested_parent" android:layout_height="80dp" android:layout_width="match_parent" android:layout_marginTop="20dp"/>

</LinearLayout>
30 changes: 30 additions & 0 deletions demo/src/main/res/layout/sample_nested_parent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/parent_framelayout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<com.daimajia.swipe.SwipeLayout
android:id="@+id/test_swipe_swipe"
android:layout_width="match_parent" android:layout_height="80dp">
<LinearLayout
android:layout_width="80dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/trash"
android:src="@drawable/trash"
android:layout_width="match_parent"
android:background="#FF3B30"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:padding="10dp"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="SeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
</FrameLayout>
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# org.gradle.parallel=true


VERSION_NAME=1.1.4
VERSION_CODE=15
VERSION_NAME=1.1.6
VERSION_CODE=17
GROUP=com.daimajia.swipelayout

ANDROID_BUILD_MIN_SDK_VERSION=8
Expand Down
71 changes: 55 additions & 16 deletions library/src/main/java/com/daimajia/swipe/SwipeLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class SwipeLayout extends FrameLayout {
private Map<View, ArrayList<OnRevealListener>> mRevealListeners = new HashMap<View, ArrayList<OnRevealListener>>();
private Map<View, Boolean> mShowEntirely = new HashMap<View, Boolean>();

private DoubleClickListener mDoubleClickListener;

private boolean mSwipeEnabled = true;

public static enum DragEdge {
Expand Down Expand Up @@ -126,7 +128,7 @@ public void addRevealListener(int childId, OnRevealListener l){
throw new IllegalArgumentException("Child does not belong to SwipeListener.");
}

if(mShowEntirely.containsKey(child) == false){
if(!mShowEntirely.containsKey(child)){
mShowEntirely.put(child, false);
}
if(mRevealListeners.get(child) == null)
Expand Down Expand Up @@ -768,6 +770,7 @@ public boolean onTouchEvent(MotionEvent event) {
}else if(status == Status.Open){
touching = getBottomView();
}

switch (action){
case MotionEvent.ACTION_DOWN:
mDragHelper.processTouchEvent(event);
Expand Down Expand Up @@ -909,7 +912,22 @@ private AdapterView getAdapterView(){
return null;
}


private void performAdapterViewItemClick(MotionEvent e){
ViewParent t = getParent();
while(t != null) {
if(t instanceof AdapterView){
AdapterView view = (AdapterView)t;
int p = view.getPositionForView(SwipeLayout.this);
if( p != AdapterView.INVALID_POSITION &&
view.performItemClick(view.getChildAt(p), p, view.getAdapter().getItemId(p)))
return;
}else{
if(t instanceof View && ((View) t).performClick())
return;
}
t = t.getParent();
}
}

private GestureDetector gestureDetector = new GestureDetector(getContext(), new SwipeDetector());
class SwipeDetector extends GestureDetector.SimpleOnGestureListener{
Expand All @@ -927,28 +945,41 @@ public boolean onDown(MotionEvent e) {
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
ViewParent t = getParent();
while(t != null) {
if(t instanceof AdapterView){
AdapterView view = (AdapterView)t;
int p = view.getPositionForView(SwipeLayout.this);
if( p != AdapterView.INVALID_POSITION &&
view.performItemClick(view.getChildAt(p), p, view.getAdapter().getItemId(p)))
return true;
}else{
if(t instanceof View && ((View) t).performClick())
return true;
}
t = t.getParent();
if(mDoubleClickListener == null){
performAdapterViewItemClick(e);
}
return false;
return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(mDoubleClickListener != null){
performAdapterViewItemClick(e);
}
return true;
}

@Override
public void onLongPress(MotionEvent e) {
performLongClick();
}

@Override
public boolean onDoubleTap(MotionEvent e) {
if(mDoubleClickListener != null){
View target;
ViewGroup bottom = getBottomView();
ViewGroup surface = getSurfaceView();
if(e.getX() > bottom.getLeft() && e.getX() < bottom.getRight()
&& e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()){
target = bottom;
}else{
target = surface;
}
mDoubleClickListener.onDoubleClick(SwipeLayout.this, target == surface);
}
return true;
}
}

public void setDragEdge(DragEdge dragEdge){
Expand Down Expand Up @@ -1250,6 +1281,14 @@ private Rect computeBottomLayDown(DragEdge dragEdge){
return new Rect(bl, bt, br, bb);
}

public void setOnDoubleClickListener(DoubleClickListener doubleClickListener){
mDoubleClickListener = doubleClickListener;
}

public interface DoubleClickListener {
public void onDoubleClick(SwipeLayout layout, boolean surface);
}

private int dp2px(float dp){
return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
}
Expand Down

0 comments on commit e0b54de

Please sign in to comment.