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

侧滑删除进阶(五)

时间:2015-08-12 19:41:10      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:getfirstvisibleposit   pointtoposition   getchildat   setlayoutparams   leftmargin   

*********************************************跟上一篇效果一样***********************************

技术分享

activity_main.xml

<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=".MainActivity" >  
      
    <com.example.listview.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>  

item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:orientation="horizontal" >  
  
    <TextView  
        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"/>  
  
    <TextView  
        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> 

MainActivity
package com.example.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
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;
import android.widget.Toast;

public class MainActivity extends Activity {
	private QQListView mListView;
	private ArrayList<String> mData = new ArrayList<String>() {
		{
			for (int i = 0; i < 50; i++) {
				add("hello world, hello android  " + i);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 记载布局,只有一个自定义listView
		setContentView(R.layout.activity_main);
		//获取listview空间
		mListView = (QQListView) findViewById(R.id.list);
		//创建并且设置适配器
		mListView.setAdapter(new MyAdapter());
		//设置条目监听事件
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			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 {

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

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

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

		@Override
		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 = position;
			delete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mData.remove(pos);
					notifyDataSetChanged();
					mListView.turnToNormal();
				}
			});

			return convertView;
		}
	}
}

QQListView
package com.example.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;

public class QQListView extends ListView {
	private int mScreenWidth; // 屏幕宽度
	private int mDownX; // 按下点的x值
	private int mDownY; // 按下点的y值
	private int mDeleteBtnWidth;// 删除按钮的宽度

	private boolean isDeleteShown; // 删除按钮是否正在显示

	private ViewGroup mPointChild; // 当前处理的item
	private LinearLayout.LayoutParams mLayoutParams; // 当前处理的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.widthPixels;
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			performActionDown(ev);
			break;
		case MotionEvent.ACTION_MOVE:
			return performActionMove(ev);
		case MotionEvent.ACTION_UP:
			performActionUp();
			break;
		}

		return super.onTouchEvent(ev);
	}

	/**
	 * 处理action_down事件
	 */
	private void performActionDown(MotionEvent ev) {
		// 如果删除按钮处于显示状态,那么就将listview处于正常状态
		if (isDeleteShown) {
			turnToNormal();
		}
		//获取按下的xy坐标值
		mDownX = (int) ev.getX();
		mDownY = (int) ev.getY();
		// 获取listview中当前点的item
		mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)
				- getFirstVisiblePosition());
		Log.e("TAG", ""+pointToPosition(mDownX, mDownY));
		Log.e("TAG", ""+getFirstVisiblePosition());
		// 获取每个条目的删除按钮的宽度
		mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;
		//获取每个条目的内容区域的布局参数--另外的方式
		mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)
				.getLayoutParams();
		// 为什么要重新设置layout_width 等于屏幕宽度
		// 因为match_parent时,不管你怎么滑,都不会显示删除按钮
		// why? 因为match_parent时,ViewGroup就不去布局剩下的view
		mLayoutParams.width = mScreenWidth;
		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;
				Log.e("TAGG", ""+scroll);
				Log.e("TAGG", ""+mDeleteBtnWidth);
				// 如果大于了删除按钮的宽度, 则最大为删除按钮的宽度
				if (-scroll >= mDeleteBtnWidth) {
					scroll = -mDeleteBtnWidth;
				}
				// 重新设置leftMargin
				mLayoutParams.leftMargin = scroll;
				mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
			}

			return true;
		}
		return super.onTouchEvent(ev);
	}

	/**
	 * 处理action_up事件
	 */
	private void performActionUp() {
		// 偏移量大于button的一半,则显示button
		// 否则恢复默认
		if (-mLayoutParams.leftMargin >= mDeleteBtnWidth / 2) {
			mLayoutParams.leftMargin = -mDeleteBtnWidth;
			isDeleteShown = true;
		} else {
			turnToNormal();
		}

		mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
	}

	/**
	 * 变为正常状态
	 */
	public void turnToNormal() {
		mLayoutParams.leftMargin = 0;
		mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
		isDeleteShown = false;
	}

	/**
	 * 当前是否可点击
	 * 
	 * @return 是否可点击
	 */
	public boolean canClick() {
		return !isDeleteShown;
	}
}



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

侧滑删除进阶(五)

标签:getfirstvisibleposit   pointtoposition   getchildat   setlayoutparams   leftmargin   

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

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