码迷,mamicode.com
首页 > 其他好文 > 详细

侧滑删除进阶(四)

时间:2015-08-12 19:40:45      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:slidelistview   pointtoposition   getchildat   motionevent   

最近在写滑动删除,网上代码又是看的云里雾里,果断自己研究,虽然效果不太好把,但是思路很清晰,代码逻辑很简单

技术分享

先来看主页面布局

activity_main.xml--很简单就一个自定义listview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.slidelistview.MainActivity" >

    <com.example.slidelistview.SlideListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</RelativeLayout>

再看看每条item的布局--很简单就2个textview(我都懒得用imageview,怕麻烦)

listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textview"
        android:gravity="center"
        android:background="#FFA07A"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/delete"
         android:gravity="center"
        android:background="#FF3E96"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="删除" />

</LinearLayout>

MainActivity
package com.example.slidelistview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

	private SlideListView listView;
	private MyAdapter myAdapter;
	private ArrayList<String> data = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myAdapter = new MyAdapter();
		for (int i = 0; i < 10; i++) {
			data.add("A");
		}
		listView = (SlideListView) findViewById(R.id.listview);
		listView.setAdapter(myAdapter);
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = View.inflate(MainActivity.this,
						R.layout.listview_item, null);
				viewHolder.textView = (TextView) convertView
						.findViewById(R.id.textview);
				viewHolder.deleTextView = (TextView) convertView
						.findViewById(R.id.delete);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.textView.setText(data.get(position));
			viewHolder.deleTextView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					data.remove(position);
					myAdapter.notifyDataSetChanged();
					listView.getChildAt(position).scrollTo(0, 0);
				}
			});
			return convertView;
		}

		class ViewHolder {
			private TextView textView;
			private TextView deleTextView;
		}
	}
}

SlideListView
package com.example.slidelistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.Scroller;
import android.widget.Toast;

public class SlideListView extends ListView {
	private Scroller scroller;

	private int downX;
	private int downY;
	private int moveX;
	private int moveY;
	private int downPosition;
	private int upPosition;
	private boolean deleOpen = false;

	private View slideListItem;
	private int lastPos = -1;

	/**
	 * 构造器
	 */
	public SlideListView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	public SlideListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public SlideListView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		scroller = new Scroller(context);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		final int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:

			downX = (int) ev.getX();
			downY = (int) ev.getY();
			// 获取按下的条目索引
			downPosition = pointToPosition(downX, downY);
			slideListItem = getChildAt(downPosition);
			if (slideListItem != null) {
				slideListItem.scrollTo(0, 0);
			}

			// 如果按下的不是当前的item
			if (lastPos != -1) {
				if (lastPos != downPosition) {
					View childAt = getChildAt(lastPos);
					childAt.scrollTo(0, 0);
				}
			}
			lastPos = downPosition;
			break;

		case MotionEvent.ACTION_MOVE:

			moveX = (int) ev.getX();
			moveY = (int) ev.getY();

			int upPosition = pointToPosition(moveX, moveY);

			if (downPosition == upPosition) {
				// 是同一个条目
				if ((downX - moveX) > 50) {
					if (slideListItem != null) {
						slideListItem.scrollTo(100, 0);
					}

				}
			}

			break;
		case MotionEvent.ACTION_UP:
			break;
		default:
			break;
		}
		return super.onTouchEvent(ev);
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

侧滑删除进阶(四)

标签:slidelistview   pointtoposition   getchildat   motionevent   

原文地址:http://blog.csdn.net/u013210620/article/details/47447215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!