通过onTouch来确定gridview点击item事件的是listView哪一个item

var sogou_ad_id=731545;
var sogou_ad_height=90;
var sogou_ad_width=980;今天看啥 热点:
仿QQ侧滑删除ListView——2015第一博,listview2015
一直感觉QQ最近联系人那个侧滑删除功能挺高大上的,经过几经波折,终于在新的一年里实现了该功能。实现这个功能真是费了老劲了,好几次有了想法,兴奋的去写代码实现,结果让代码打了自己一个耳光,最终还是用margin的方式实现了这种效果,好吧, 先上效果!
看完效果,就来说一下思路吧:
1、item的左右滑动效果我是用的magin实现的。
2、虽然item布局的时候文本TextView的宽度设置的是match_parent,但在点下去的时候就将这个值设置为了固定值:屏幕的宽度
3、通过提供一个方法来处理滑动和外部itemClick的冲突
主要代码:
public class QQListView extends ListView {
private int mScreenW // 屏幕宽度
private int mDownX;
// 按下点的x值
private int mDownY;
// 按下点的y值
private int mDeleteBtnW// 删除按钮的宽度
private boolean isDeleteS // 删除按钮是否正在显示
private ViewGroup mPointC // 当前处理的item
private LinearLayout.LayoutParams mLayoutP // 当前处理的item的LayoutParams
public QQListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public QQListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 获取屏幕宽度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthP
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
performActionDown(ev);
case MotionEvent.ACTION_MOVE:
return performActionMove(ev);
case MotionEvent.ACTION_UP:
performActionUp();
return super.onTouchEvent(ev);
// 处理action_down事件
private void performActionDown(MotionEvent ev) {
if(isDeleteShown) {
turnToNormal();
mDownX = (int) ev.getX();
mDownY = (int) ev.getY();
// 获取当前点的item
mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)
- getFirstVisiblePosition());
// 获取删除按钮的宽度
mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().
mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)
.getLayoutParams();
// 为什么要重新设置layout_width 等于屏幕宽度
// 因为match_parent时,不管你怎么滑,都不会显示删除按钮
// why? 因为match_parent时,ViewGroup就不去布局剩下的view
mLayoutParams.width = mScreenW
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
// 处理action_move事件
private boolean performActionMove(MotionEvent ev) {
int nowX = (int) ev.getX();
int nowY = (int) ev.getY();
if(Math.abs(nowX - mDownX) & Math.abs(nowY - mDownY)) {
// 如果向左滑动
if(nowX & mDownX) {
// 计算要偏移的距离
int scroll = (nowX - mDownX) / 2;
// 如果大于了删除按钮的宽度, 则最大为删除按钮的宽度
if(-scroll &= mDeleteBtnWidth) {
scroll = -mDeleteBtnW
// 重新设置leftMargin
mLayoutParams.leftMargin =
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
return super.onTouchEvent(ev);
// 处理action_up事件
private void performActionUp() {
// 偏移量大于button的一半,则显示button
// 否则恢复默认
if(-mLayoutParams.leftMargin &= mDeleteBtnWidth / 2) {
mLayoutParams.leftMargin = -mDeleteBtnW
isDeleteShown =
turnToNormal();
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
* 变为正常状态
public void turnToNormal() {
mLayoutParams.leftMargin = 0;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
isDeleteShown =
* 当前是否可点击
* @return 是否可点击
public boolean canClick() {
return !isDeleteS
很显然, 肯定要选择重写ListView来实现这种效果,并且重写onTouchEvent,通过判断move来达到侧滑效果。
先看看构造方法:
public QQListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 获取屏幕宽度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthP
在构造方法中就干了一件事:获取屏幕的宽度, 为什么要获取屏幕宽度呢? 上面已经说过了,这里要改变一下item里的第一个TextView的layout_width为固定值。 match_parent不是很好吗? 为什么要多次一举重新设置为屏幕宽度呢?答案是:当前一个View的layout_width为match_parent时,ViewGroup就不去理会剩下的View了,也就是删除的那个按钮根本没有绘制出来!
下面的onTouchEvent中,分别在ACTION_DOWN、ACTION_MOVE、ACTION_UP三个case中调用了三个方法来处理这三个事件。
首先看看在DOWN的时候做了什么。
// 处理action_down事件
private void performActionDown(MotionEvent ev) {
if(isDeleteShown) {
turnToNormal();
mDownX = (int) ev.getX();
mDownY = (int) ev.getY();
// 获取当前点的item
mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)
- getFirstVisiblePosition());
// 获取删除按钮的宽度
mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().
mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)
.getLayoutParams();
// 为什么要重新设置layout_width 等于屏幕宽度
// 因为match_parent时,不管你怎么滑,都不会显示删除按钮
// why? 因为match_parent时,ViewGroup就不去布局剩下的view
mLayoutParams.width = mScreenW
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
3~5行,如果某一个item的deleteButton正在显示,则调用turnToNormal方法去恢复现场,turnToNormal方法其实很简单,稍候说明一下。
7~11行的任务就是确定当前按下的点所在哪个item上,并获取这个item。这里需要注意的pointToPosition方法获取的是当前点在所有item中第几个,而getChildAt()获取的是距离第一个可见项的第几个,所以要减去getFirstVisiblePosition()才能得到正确的item。
13行,获取了deleteButton的宽度,这个宽度是在下面判断deleteButton是否要全部显示或隐藏用的。
最主要的14~25行,重新设置第一个TextView的layout_width,至于为什么,上面已经说过了。
处理move事件要稍微麻烦点。
// 处理action_move事件
private boolean performActionMove(MotionEvent ev) {
int nowX = (int) ev.getX();
int nowY = (int) ev.getY();
if(Math.abs(nowX - mDownX) & Math.abs(nowY - mDownY)) {
// 如果向左滑动
if(nowX & mDownX) {
// 计算要偏移的距离
int scroll = (nowX - mDownX) / 2;
// 如果大于了删除按钮的宽度, 则最大为删除按钮的宽度
if(-scroll &= mDeleteBtnWidth) {
scroll = -mDeleteBtnW
// 重新设置leftMargin
mLayoutParams.leftMargin =
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
return super.onTouchEvent(ev);
performActionMove是有返回值的,而且我们在onTouchEvent中return了它的返回值,有返回值的目的就是让横向滑动的时候在我们的onTouchEvent中消费了事件,竖屏滑动的时候交由ListView的onTouchEvent处理事件,从而避免屏蔽掉了ListView的上下滑动机制。
3~4行,首先获取当前手指所在的点。
然后第5行去判断x轴方向的位移是否大于y轴方向的位移,如果不大于,move事件直接交由super处理。
再来看看if内部,接着又是一个if, 这里主要是判断是不是向左滑动,如果向左滑动, 在第9行计算需要的偏移量,这里取的是手指偏移量的一半。
10~13行主要是为了防止滑动过界,右边出现空白的情况。
15~16行,就是改变第一个TextView的leftMargin的值,从而达到向左移动的效果。
处理up就简单多了。
// 处理action_up事件
private void performActionUp() {
// 偏移量大于button的一半,则显示button
// 否则恢复默认
if(-mLayoutParams.leftMargin &= mDeleteBtnWidth / 2) {
mLayoutParams.leftMargin = -mDeleteBtnW
isDeleteShown =
turnToNormal();
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
主要就是通过判断view的leftMargin来确定deletebutton是要进入显示状态还是不显示状态。
很多地方都用到了turnToNormal这个方法,那我们就来看看这个自定义的方法。
public void turnToNormal() {
mLayoutParams.leftMargin = 0;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
isDeleteShown =
这个自定义方法是一个public的,并不是我没有注意代码的封装性,而是这个方法在外部也会使用到, turnToNormal要做的事也很简单,就是“恢复现场”。
还一个简单的自定义方法,在外部需要itemClick的时候,通过该方法判断是否当前是否处于item可点击状态。
public boolean canClick() {
return !isDeleteS
好啦, 接下来是应用环节了,看两个布局文件,一个是main布局,一个是ListView的item布局
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:context=&.MainActivity& &
&org.loader.qqlist.QQListView
android:id=&@+id/list&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:cacheColorHint=&@android:color/transparent&
android:listSelector=&@android:color/transparent&
android:divider=&@android:color/darker_gray&
android:dividerHeight=&2dp& /&
&/RelativeLayout&
这个没什么好说的了, 就是引用了自定义的ListView。
来看看item的布局
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal& &
android:id=&@+id/tv&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:paddingBottom=&20dp&
android:paddingLeft=&10dp&
android:paddingTop=&20dp&
android:background=&@android:color/white&/&
android:id=&@+id/delete&
android:layout_width=&80dp&
android:layout_height=&match_parent&
android:background=&#FFFF0000&
android:gravity=&center&
android:paddingLeft=&20dp&
android:textColor=&@android:color/white&
android:paddingRight=&20dp&
android:text=&删除& /&
&/LinearLayout&
哎? 不是说第一个TextView的layout_width要动态设置成固定值:屏幕的宽度吗? 这里应该wrap_content也可以吧? 答案是不可以! 回想一下,去设置固定值是在处理到该item的事件时才干的活,那你从没有点击的item呢? 肯定还是需要match_parent的。
最后看看Activity
public class MainActivity extends Activity {
private QQListView mListV
private ArrayList&String& mData = new ArrayList&String&() {
for(int i=0;i&50;i++) {
add(&hello world, hello android
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (QQListView) findViewById(R.id.list);
mListView.setAdapter(new MyAdapter());
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& parent, View view,
int position, long id) {
if(mListView.canClick()) {
Toast.makeText(MainActivity.this, mData.get(position), Toast.LENGTH_SHORT).show();
class MyAdapter extends BaseAdapter {
public int getCount() {
return mData.size();
public Object getItem(int position) {
return mData.get(position);
public long getItemId(int position) {
public View getView(int position, View convertView, ViewGroup parent) {
if(null == convertView) {
convertView = View.inflate(MainActivity.this, R.layout.item, null);
TextView tv = (TextView) convertView.findViewById(R.id.tv);
TextView delete = (TextView) convertView.findViewById(R.id.delete);
tv.setText(mData.get(position));
final int pos =
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mData.remove(pos);
notifyDataSetChanged();
mListView.turnToNormal();
return convertV
62行, 我们在deleteButton的onClick事件中调用了自定义方法turnToNormal,这样就保证了删除后,deleteButton不会继续存在下一个item上。
22~24行, 多了一个判断,通过canClick方法来判断当前item是否可点击。
最后是源码下载地址:http://git.oschina.net/qibin/horizontalScrollListView
相关搜索:
相关阅读:
相关频道:
Android教程最近更新博客分类:
SwipeLayout
实现listview滑动删除功能
首先设置listView.setOnTouchListener(onTouchListener);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(onItemClickListener);
listView.setOnTouchListener(onTouchListener);
然后实现onTouchListener
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
float x, y, ux,
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
case MotionEvent.ACTION_UP:
ux = event.getX();
uy = event.getY();
index = ((ListView)v).pointToPosition((int) x, (int) y);//item的position
int p2 = ((ListView)v).pointToPosition((int) ux, (int) uy);
if (index == p2 && Math.abs(x - ux) & 20) {
notifyDataSetChanged();
最后在Adapter中如下实现:
class ListViewAdapter extends BaseAdapter {
public ListViewAdapter() {
public int getCount() {
// TODO Auto-generated method stub
return orders.size();
public Object getItem(int position) {
// TODO Auto-generated method stub
return orders.get(position);
public long getItemId(int position) {
// TODO Auto-generated method stub
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
hodler = new ViewHodler();
convertView = LayoutInflater.from(context).inflate(R.layout.simple_item_4_for_my_order, null);
hodler.item_0 = (TextView) convertView.findViewById(R.id.item_0);
hodler.item_1 = (TextView) convertView.findViewById(R.id.item_1);
hodler.item_2 = (TextView) convertView.findViewById(R.id.item_2);
hodler.item_3 = (Button) convertView.findViewById(R.id.item_3);
hodler.item_4 = (TextView) convertView.findViewById(R.id.item_4);
convertView.setTag(hodler);
hodler = (ViewHodler) convertView.getTag();
final Order instance = orders.get(position);
hodler.item_0.setText(instance.getStoreName());
hodler.item_1.setText("$"+instance.getPaidAmt()+" for "+instance.getMenuNum()+" items");
hodler.item_2.setText(instance.getCreateTime());
if(position==index){
if(hodler.item_3.getVisibility()==View.VISIBLE){
hodler.item_3.setVisibility(View.GONE);
doAnimation(context, hodler.item_3, R.anim.push_right_out);
hodler.item_3.setVisibility(View.VISIBLE);
doAnimation(context, hodler.item_3, R.anim.push_right_in);
hodler.item_3.setText("Delete");
hodler.item_3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
App.order=
startCommand(context, handler, MAND_DELETE_ORDER);
hodler.item_3.setVisibility(View.GONE);
return convertV
class ViewHodler {
TextView item_0;
TextView item_1;
TextView item_2;
Button item_3;
TextView item_4;
Adapter布局:
&FrameLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:descendantFocusability="blocksDescendants"
&LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
&LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" &
android:id="@+id/item_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="@dimen/font_middle"
android:textStyle="bold"
android:layout_weight="1"
android:singleLine="true"
android:id="@+id/item_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="@dimen/font_xsmall" /&
&/LinearLayout&
&LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" &
android:id="@+id/item_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:textSize="@dimen/font_xsmall" /&
android:id="@+id/item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="@dimen/font_xsmall" /&
&/LinearLayout&
&/LinearLayout&
android:id="@+id/item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:text="Delete"
android:textColor="@color/white"
android:textSize="@dimen/font_middle"
android:visibility="gone"
android:background="@drawable/bg_btn_red"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
&/FrameLayout&
2个动画文件R.anim.push_right_out,R.anim.push_right_in:
&set xmlns:android="/apk/res/android"&
&translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="300"/&
&alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /&
&set xmlns:android="/apk/res/android"&
&translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/&
&alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /&
另一种手势滑动直接删除的例子:
http://download.csdn.net/detail/xiaanming/6790849
import android.content.C
import android.util.AttributeS
import android.view.MotionE
import android.view.VelocityT
import android.view.V
import android.view.ViewC
import android.widget.AdapterV
import android.widget.ListV
import android.widget.S
public class SlideCutListView extends ListView {
* 当前滑动的ListView position
private int slideP
* 手指按下X的坐标
private int downY;
* 手指按下Y的坐标
private int downX;
* 屏幕宽度
// private int screenW
* ListView的item
private View itemV
private static final int SNAP_VELOCITY = 600;
* 速度追踪对象
private VelocityTracker velocityT
* 是否响应滑动,默认为不响应
private boolean isSlide =
* 认为是用户滑动的最小距离
private int mTouchS
移除item后的回调接口
private OnItemRemovedListener onItemRemovedL
* 用来指示item滑出屏幕的方向,向左或者向右,用一个枚举值来标记
private SlideDirection removeD
// 滑动删除方向的枚举值
public enum SlideDirection {
RIGHT, LEFT;
public SlideCutListView(Context context) {
this(context, null);
public SlideCutListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public SlideCutListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
screenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
scroller = new Scroller(context);
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
* 分发事件,主要做的是判断点击的是那个item, 以及通过postDelayed来设置响应左右滑动事件
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// 假如scroller滚动还没有结束,我们直接返回
if (!scroller.isFinished()) {
return super.dispatchTouchEvent(event);
addVelocityTracker(event);
downX = (int) event.getX();
downY = (int) event.getY();
slidePosition = pointToPosition(downX, downY);
// 无效的position, 不做任何处理
if (slidePosition == AdapterView.INVALID_POSITION) {
return super.dispatchTouchEvent(event);
// 获取我们点击的item view
itemView = getChildAt(slidePosition - getFirstVisiblePosition());
case MotionEvent.ACTION_MOVE: {
if (Math.abs(getXVelocity()) & SNAP_VELOCITY
|| (Math.abs(event.getX() - downX) & mTouchSlop && Math
.abs(event.getY() - downY) & mTouchSlop)) {
case MotionEvent.ACTION_UP:
recycleVelocityTracker();
return super.dispatchTouchEvent(event);
* 往右滑动,getScrollX()返回的是左边缘的距离,就是以View左边缘为原点到开始滑动的距离,所以向右边滑动为负值
private void scrollRight() {
removeDirection = SlideDirection.RIGHT;
final int delta = (getWidth() + itemView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
scroller.startScroll(itemView.getScrollX(), 0, -delta, 0,
Math.abs(delta));
postInvalidate(); // 刷新itemView
* 向左滑动,根据上面我们知道向左滑动为正值
private void scrollLeft() {
removeDirection = SlideDirection.LEFT;
final int delta = (getWidth() - itemView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
scroller.startScroll(itemView.getScrollX(), 0, delta, 0,
Math.abs(delta));
postInvalidate(); // 刷新itemView
* 根据手指滚动itemView的距离来判断是滚动到开始位置还是向左或者向右滚动
private void scrollByDistanceX() {
// 如果向左滚动的距离大于屏幕的三分之一,就让其删除
if (itemView.getScrollX() &= getWidth() / 3) {
scrollLeft();
} else if (itemView.getScrollX() &= -getWidth() / 3) {
scrollRight();
// 滚回到原始位置,为了偷下懒这里是直接调用scrollTo滚动
itemView.scrollTo(0, 0);
* 处理我们拖动ListView item的逻辑
public boolean onTouchEvent(MotionEvent ev) {
if (isSlide && slidePosition != AdapterView.INVALID_POSITION) {
addVelocityTracker(ev);
final int action = ev.getAction();
int x = (int) ev.getX();
switch (action) {
case MotionEvent.ACTION_MOVE:
int deltaX = downX -
// 手指拖动itemView滚动, deltaX大于0向左滚动,小于0向右滚
itemView.scrollBy(deltaX, 0);
case MotionEvent.ACTION_UP:
int velocityX = getXVelocity();
if (velocityX & SNAP_VELOCITY) {
scrollRight();
} else if (velocityX & -SNAP_VELOCITY) {
scrollLeft();
scrollByDistanceX();
recycleVelocityTracker();
// 手指离开的时候就不响应左右滚动
// 拖动的时候ListView不滚动
//否则直接交给ListView来处理onTouchEvent事件
return super.onTouchEvent(ev);
public void computeScroll() {
// 调用startScroll的时候puteScrollOffset()返回true,
if (puteScrollOffset()) {
// 让ListView item根据当前的滚动偏移量进行滚动
itemView.scrollTo(scroller.getCurrX(), scroller.getCurrY());
postInvalidate();
// 滚动动画结束的时候调用回调接口
if (scroller.isFinished()) {
if (onItemRemovedListener == null) {
throw new NullPointerException("OnItemRemovedListener is null, we should called setOnItemRemovedListener()");
itemView.scrollTo(0, 0);
onItemRemovedListener.onItemRemoved(removeDirection, slidePosition);
* 添加用户的速度跟踪器
* @param event
private void addVelocityTracker(MotionEvent event) {
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
velocityTracker.addMovement(event);
* 移除用户速度跟踪器
private void recycleVelocityTracker() {
if (velocityTracker != null) {
velocityTracker.recycle();
velocityTracker =
* 获取X方向的滑动速度,大于0向右滑动,反之向左
private int getXVelocity() {
puteCurrentVelocity(1000);
int velocity = (int) velocityTracker.getXVelocity();
* 设置滑动删除的回调接口
* @param removeListener
public void setOnItemRemovedListener(OnItemRemovedListener onItemRemovedListener) {
this.onItemRemovedListener = onItemRemovedL
* 当ListView item滑出屏幕,回调这个接口
* 我们需要在回调方法removeItem()中移除该Item,然后刷新ListView
public interface OnItemRemovedListener {
public void onItemRemoved(SlideDirection direction, int position);
import java.util.ArrayL
import java.util.L
import android.app.A
import android.os.B
import android.view.V
import android.widget.AdapterV
import android.widget.AdapterView.OnItemClickL
import android.widget.ArrayA
import android.widget.T
import com.example.slidecutlistview.SlideCutListView.OnItemRemovedL
import com.example.slidecutlistview.SlideCutListView.SlideD
public class MainActivity extends Activity implements OnItemRemovedListener{
private SlideCutListView slideCutListV
private ArrayAdapter&String&
private List&String& datas = new ArrayList&String&();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private void init() {
slideCutListView = (SlideCutListView) findViewById(R.id.slideCutListView);
slideCutListView.setOnItemRemovedListener(this);
for(int i=0; i&40; i++){
datas.add("item" + i);
adapter = new ArrayAdapter&String&(this, R.layout.listview_item, R.id.list_item, datas);
slideCutListView.setAdapter(adapter);
slideCutListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, datas.get(position), Toast.LENGTH_SHORT).show();
public void onItemRemoved(SlideDirection direction, int position) {
adapter.remove(adapter.getItem(position));
switch (direction) {
case RIGHT:
Toast.makeText(this, "position="+ position, Toast.LENGTH_SHORT).show();
case LEFT:
Toast.makeText(this, "position="+ position, Toast.LENGTH_SHORT).show();
仿微信的删除,效果还可以,就是封装的不够,代码之间瓜葛太大:
实现listview中item滑动效果:Swipemenulistview
下载次数: 225
下载次数: 243
doAnimation方法呢?protected void doAnimation(Context context, View view, int animId) {
Animation animation = AnimationUtils.loadAnimation(context, animId);
view.startAnimation(animation); }
gundumw100
浏览: 4202914 次
来自: 上海
本地lua脚本终于执行成功了,虽然不是通过redis
大神://处理返回的接收状态
这个好像没有监听到
拦截部分地址,怎么写的for(int i=0;i&lis ...
Android控件之带清空按钮(功能)的AutoComplet ...
希望有表例子更好。。。,不过也看明白了。
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 gridview点击item事件 的文章

 

随机推荐