如何在android中使用滑动删除listview中的行
我们在android中使用ListView。我想通过在android中使用滑动来删除ListView中的行。但是我不知道这个主题。请有人帮助我。SwipeListView.java如何在android中使用滑动删除listview中的行,android,Android,我们在android中使用ListView。我想通过在android中使用滑动来删除ListView中的行。但是我不知道这个主题。请有人帮助我。SwipeListView.java public class SwipeListView { MotionEvent last_motion; public static interface SwipeListViewCallback { public abstract ListView getListView()
public class SwipeListView {
MotionEvent last_motion;
public static interface SwipeListViewCallback {
public abstract ListView getListView();
/**
*
* @param isRight
* Swiping direction
* @param position
* which item position is swiped
*/
public abstract void onSwipeItem(boolean isRight, int position);
/**
* For single tap/Click
*
* @param adapter
* @param position
*/
public abstract void onItemClickListener(ListAdapter adapter,
int position);
}
Context m_Context;
ListView mListView;
SwipeListViewCallback m_Callback;
public SwipeListView(Context mContext, SwipeListViewCallback callback) {
// TODO Auto-generated constructor stub
if (callback == null) {
//
try {
throw new Exception(
"Activity must be implement SwipeListViewCallback");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
init(mContext, callback);
}
public SwipeListView(Context mContext) throws Exception {
// TODO Auto-generated constructor stub
if (!(mContext instanceof SwipeListViewCallback)) {
//
throw new Exception(
"Activity must be implement SwipeListViewCallback");
}
init(mContext, (SwipeListViewCallback) mContext);
}
private ListView list;
private int REL_SWIPE_MIN_DISTANCE;
private int REL_SWIPE_MAX_OFF_PATH;
private int REL_SWIPE_THRESHOLD_VELOCITY;
protected void init(Context mContext, SwipeListViewCallback mCallback) {
m_Context = mContext;
m_Callback = mCallback;
}
public void exec() {
//
DisplayMetrics dm = m_Context.getResources().getDisplayMetrics();
REL_SWIPE_MIN_DISTANCE = (int) (100.0f * dm.densityDpi / 160.0f + 0.5);
REL_SWIPE_MAX_OFF_PATH = (int) (250.0f * dm.densityDpi / 160.0f + 0.5);
REL_SWIPE_THRESHOLD_VELOCITY = (int) (100.0f * dm.densityDpi / 160.0f + 0.5);
//
list = m_Callback.getListView();
final GestureDetector gestureDetector = new GestureDetector(m_Context,
new MyGestureDetector());
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
list.setOnTouchListener(gestureListener);
// list.setOnTouchListener(new OnTouchListener() {
//
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// // TODO Auto-generated method stub
// return gestureDetector.onTouchEvent(event);
// }
// });
}
private void myOnItemClick(int position) {
if (position < 0)
return;
m_Callback.onItemClickListener(list.getAdapter(), position);
}
class MyGestureDetector extends SimpleOnGestureListener {
private int temp_position = -1;
/*
* @Override public boolean onScroll(MotionEvent e1, MotionEvent e2,
* float distanceX, float distanceY) { // TODO Auto-generated method
* stub
*
*
* super.onScroll(e1, e2, distanceX, distanceY); return false; }
*/
// Detect a single-click and call my own handler.
@Override
public boolean onSingleTapUp(MotionEvent e) {
int pos = list.pointToPosition((int) e.getX(), (int) e.getY());
myOnItemClick(pos);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
temp_position = list
.pointToPosition((int) e.getX(), (int) e.getY());
// return super.onDown(e);
last_motion = e;
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1 == null || e2 == null) {
if (last_motion != null) {
e1 = last_motion;
}
}
if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
return false;
if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
int pos = list
.pointToPosition((int) e1.getX(), (int) e2.getY());
if (pos >= 0 && temp_position == pos) {
m_Callback.onSwipeItem(true, pos);
}
} else if (e2.getX() - e1.getX() < REL_SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
int pos = list
.pointToPosition((int) e1.getX(), (int) e2.getY());
if (pos >= 0 && temp_position == pos)
m_Callback.onSwipeItem(false, pos);
}
/*
* else if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH
* && Math.abs(velocityY) > REL_SWIPE_THRESHOLD_VELOCITY) {
* System.out.println("upward down"); } else if (Math.abs(e2.getY()
* - e1.getY()) > REL_SWIPE_MAX_OFF_PATH && Math.abs(velocityY) >
* REL_SWIPE_THRESHOLD_VELOCITY) {
* System.out.println("upward down"); }
*/
return false;
}
}
}
friendlistadapter = new FriendListAdapter(getActivity(),
R.layout.friends_list_item, listdata);
lv_friendlistview.setAdapter(friendlistadapter);
SwipeListView swipeListView = new SwipeListView(getActivity(), this);
swipeListView.exec();
FriendListadapter.java
public class SwipeListView {
MotionEvent last_motion;
public static interface SwipeListViewCallback {
public abstract ListView getListView();
/**
*
* @param isRight
* Swiping direction
* @param position
* which item position is swiped
*/
public abstract void onSwipeItem(boolean isRight, int position);
/**
* For single tap/Click
*
* @param adapter
* @param position
*/
public abstract void onItemClickListener(ListAdapter adapter,
int position);
}
Context m_Context;
ListView mListView;
SwipeListViewCallback m_Callback;
public SwipeListView(Context mContext, SwipeListViewCallback callback) {
// TODO Auto-generated constructor stub
if (callback == null) {
//
try {
throw new Exception(
"Activity must be implement SwipeListViewCallback");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
init(mContext, callback);
}
public SwipeListView(Context mContext) throws Exception {
// TODO Auto-generated constructor stub
if (!(mContext instanceof SwipeListViewCallback)) {
//
throw new Exception(
"Activity must be implement SwipeListViewCallback");
}
init(mContext, (SwipeListViewCallback) mContext);
}
private ListView list;
private int REL_SWIPE_MIN_DISTANCE;
private int REL_SWIPE_MAX_OFF_PATH;
private int REL_SWIPE_THRESHOLD_VELOCITY;
protected void init(Context mContext, SwipeListViewCallback mCallback) {
m_Context = mContext;
m_Callback = mCallback;
}
public void exec() {
//
DisplayMetrics dm = m_Context.getResources().getDisplayMetrics();
REL_SWIPE_MIN_DISTANCE = (int) (100.0f * dm.densityDpi / 160.0f + 0.5);
REL_SWIPE_MAX_OFF_PATH = (int) (250.0f * dm.densityDpi / 160.0f + 0.5);
REL_SWIPE_THRESHOLD_VELOCITY = (int) (100.0f * dm.densityDpi / 160.0f + 0.5);
//
list = m_Callback.getListView();
final GestureDetector gestureDetector = new GestureDetector(m_Context,
new MyGestureDetector());
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
list.setOnTouchListener(gestureListener);
// list.setOnTouchListener(new OnTouchListener() {
//
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// // TODO Auto-generated method stub
// return gestureDetector.onTouchEvent(event);
// }
// });
}
private void myOnItemClick(int position) {
if (position < 0)
return;
m_Callback.onItemClickListener(list.getAdapter(), position);
}
class MyGestureDetector extends SimpleOnGestureListener {
private int temp_position = -1;
/*
* @Override public boolean onScroll(MotionEvent e1, MotionEvent e2,
* float distanceX, float distanceY) { // TODO Auto-generated method
* stub
*
*
* super.onScroll(e1, e2, distanceX, distanceY); return false; }
*/
// Detect a single-click and call my own handler.
@Override
public boolean onSingleTapUp(MotionEvent e) {
int pos = list.pointToPosition((int) e.getX(), (int) e.getY());
myOnItemClick(pos);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
temp_position = list
.pointToPosition((int) e.getX(), (int) e.getY());
// return super.onDown(e);
last_motion = e;
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1 == null || e2 == null) {
if (last_motion != null) {
e1 = last_motion;
}
}
if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
return false;
if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
int pos = list
.pointToPosition((int) e1.getX(), (int) e2.getY());
if (pos >= 0 && temp_position == pos) {
m_Callback.onSwipeItem(true, pos);
}
} else if (e2.getX() - e1.getX() < REL_SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
int pos = list
.pointToPosition((int) e1.getX(), (int) e2.getY());
if (pos >= 0 && temp_position == pos)
m_Callback.onSwipeItem(false, pos);
}
/*
* else if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH
* && Math.abs(velocityY) > REL_SWIPE_THRESHOLD_VELOCITY) {
* System.out.println("upward down"); } else if (Math.abs(e2.getY()
* - e1.getY()) > REL_SWIPE_MAX_OFF_PATH && Math.abs(velocityY) >
* REL_SWIPE_THRESHOLD_VELOCITY) {
* System.out.println("upward down"); }
*/
return false;
}
}
}
friendlistadapter = new FriendListAdapter(getActivity(),
R.layout.friends_list_item, listdata);
lv_friendlistview.setAdapter(friendlistadapter);
SwipeListView swipeListView = new SwipeListView(getActivity(), this);
swipeListView.exec();
在适配器类中
public void onSwipeItem(boolean isRight, int position) {
if (isRight == false) {
DELETE_POS = position;
notifyDataSetChanged();
} else if (DELETE_POS == position) {
DELETE_POS = INVALID;
notifyDataSetChanged();
}
}
在适配器类的onCreateView方法中编写喜爱的代码
if (DELETE_POS == position)
{
friendholder.iv_fr_delete.setVisibility(View.VISIBLE);
} else {
friendholder.iv_fr_delete.setVisibility(View.GONE);
}
friendholder.iv_fr_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listdata.remove(position);
friendholder.iv_fr_delete.setVisibility(View.GONE);
DELETE_POS = INVALID;
notifyDataSetChanged();
}
});
检查这个