我正在尝试创建一个列表视图,如果从右侧滑动一行,则会出现一个按钮。我浏览了 Stackoverflow 中关于类似主题的各种帖子,但无法理解这些内容。如果我是 Android 开发的新手,如果我没有什么指示去做,那就太好了。提前致谢
问问题
4349 次
2 回答
0
试试这个方法
1)创建这个类
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;
public class SmartGestureListener extends SimpleOnGestureListener implements
OnTouchListener {
private Context context;
GestureDetector gDetector;
private ListView lstCustomListView;
private boolean isFling = false;;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int swipeDeleteIjoomerButtonID;
private OnSwipeDelete local;
public SmartGestureListener() {
super();
}
public SmartGestureListener(Context context, ListView lstCustomListView,
int swipeDeleteIjoomerButtonID, OnSwipeDelete target) {
this(context, lstCustomListView, null, swipeDeleteIjoomerButtonID, target);
}
public SmartGestureListener(Context context, ListView lstCustomListView,
GestureDetector gDetector, final int swipeDeleteButttonID,
final OnSwipeDelete target) {
this.swipeDeleteIjoomerButtonID = swipeDeleteButttonID;
this.local = target;
if (gDetector == null)
gDetector = new GestureDetector(context, this);
this.context = context;
this.gDetector = gDetector;
this.lstCustomListView = lstCustomListView;
this.lstCustomListView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (!isFling) {
Log.e("tag", "onScroll");
int childern = SmartGestureListener.this.lstCustomListView
.getChildCount();
Log.e("tag", "count : " + childern);
for (int i = 0; i < childern; i++) {
View v = SmartGestureListener.this.lstCustomListView
.getChildAt(i);
Log.e("tag", "inside for loop");
if (v != null) {
Button btnDelete = (Button) v
.findViewById(swipeDeleteButttonID);
if (btnDelete != null) {
Log.e("tag", "IjoomerButton found");
if (btnDelete.getVisibility() == View.VISIBLE) {
btnDelete.setVisibility(View.INVISIBLE);
} else {
btnDelete.setVisibility(View.INVISIBLE);
}
} else {
Log.e("tag", "IjoomerButton not found");
}
}
{
Log.e("tag", "view not found");
}
}
} else {
isFling = false;
}
}
});
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
super.onFling(e1, e2, velocityX, velocityY);
try {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
isFling = true;
if (lstCustomListView != null) {
final int adapterIndex = lstCustomListView.pointToPosition(
(int) e1.getX(), (int) e1.getY());
int firstViewItemIndex = lstCustomListView
.getFirstVisiblePosition();
int viewIndex = adapterIndex - firstViewItemIndex;
Log.e("indexes", adapterIndex + " : " + firstViewItemIndex
+ " : " + viewIndex);
View v = lstCustomListView.getChildAt(viewIndex);
Button btnDelete = (Button) v
.findViewById(this.swipeDeleteIjoomerButtonID);
// flip in animation applied to the IjoomerButton
if (btnDelete.getVisibility() == View.INVISIBLE) {
btnDelete.setVisibility(View.VISIBLE);
Animation a = AnimationUtils.loadAnimation(context,
R.anim.slide_left_in);
a.reset();
btnDelete.clearAnimation();
btnDelete.startAnimation(a);
btnDelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
local.actionSwipeDelete(adapterIndex);
}
});
} else {
btnDelete.setVisibility(View.INVISIBLE);
}
}
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
isFling = true;
if (lstCustomListView != null) {
final int adapterIndex = lstCustomListView.pointToPosition(
(int) e1.getX(), (int) e1.getY());
int firstViewItemIndex = lstCustomListView
.getFirstVisiblePosition();
final int viewIndex = adapterIndex - firstViewItemIndex;
Log.e("indexes", adapterIndex + " : " + firstViewItemIndex
+ " : " + viewIndex);
View v = lstCustomListView.getChildAt(viewIndex);
Button btnDelete = (Button) v
.findViewById(swipeDeleteIjoomerButtonID);
if (btnDelete.getVisibility() == View.INVISIBLE) {
btnDelete.setVisibility(View.VISIBLE);
Animation a = AnimationUtils.loadAnimation(context,
R.anim.slide_left_in);
a.reset();
btnDelete.clearAnimation();
btnDelete.startAnimation(a);
btnDelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
local.actionSwipeDelete(adapterIndex);
}
});
} else {
btnDelete.setVisibility(View.INVISIBLE);
}
}
return true;
}
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
public boolean onTouch(View v, MotionEvent event) {
Log.e("gesture", "onTouch");
return gDetector.onTouchEvent(event);
}
public GestureDetector getDetector() {
return gDetector;
}
}
2)动画Xml文件(在“res”文件夹中创建“anim”文件夹并在下面创建动画文件)
slide_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>
</set>
3)如何使用?
SmartGestureListener swipeGestureListener = new SmartGestureListener(this, myListview, R.id.btnDelete, new OnSwipeDelete() {
@Override
public void actionSwipeDelete(int rowID) {
}
});
于 2013-11-11T08:57:41.327 回答
0
于 2013-11-11T07:43:00.693 回答