Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義View的學習(四)

Android自定義View的學習(四)

編輯:關於Android編程

MainActivity如下:

package cc.testviewstudy4;

import java.util.ArrayList;
import java.util.HashMap;
import cc.testviewstudy4.ListViewSubClass.OnDeleteListener;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.app.Activity;
/**
 * Demo描述:
 * 關於自定義View的學習(四)
 * 
 * 自定義View的實現方式大概可以分為三種:
 * 自繪控件、組合控件、以及繼承控件
 * 在此Demo中實現繼承控件:
 * 自定義ListView.在ListView的Item上滑動時可出現一個
 * 刪除View.點擊它,可刪除ListView中的該條數據
 * 
 * 學習資料:
 * http://blog.csdn.net/guolin_blog/article/details/17357967
 * Thank you very much
 * 
 */
public class MainActivity extends Activity {
    private ListViewSubClass mListView;
    private SimpleAdapter mSimpleAdapter;
    private ArrayList> mArrayList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mListView=(ListViewSubClass) findViewById(R.id.listView);
		mArrayList=new ArrayList>();
      	HashMap hashMap=null;
      	for (int i = 0; i <12; i++) {
      		hashMap=new HashMap();
      		hashMap.put("name", "小明"+i);
        	mArrayList.add(hashMap);
		}
      mSimpleAdapter=new SimpleAdapter(this, mArrayList, R.layout.listviewitem, 
    		                           new String []{"name"}, new int[]{R.id.name});
      mListView.setAdapter(mSimpleAdapter);
      mListView.setOnDeleteListener(new OnDeleteListener() {
		@Override
		public void onDelete(int index) {
			mArrayList.remove(index);
			mSimpleAdapter.notifyDataSetChanged();
		}
	});
	}
}


ListViewSubClass如下:

package cc.testviewstudy4;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ListViewSubClass extends ListView implements OnGestureListener,OnTouchListener {
	private GestureDetector mGestureDetector;
	private OnDeleteListener mDeleteListener;
	private View mDeleteView;
	private ViewGroup mListViewItemViewGroup;
	private int selectedItem;
	private boolean isDeleteShowing;
	
	public ListViewSubClass(Context context) {
		super(context);
	}
	
	public ListViewSubClass(Context context, AttributeSet attrs) {
		super(context, attrs);
		// 監聽Touch
		setOnTouchListener(this);
		// 手勢
		mGestureDetector = new GestureDetector(context, this);
	}
	
	public ListViewSubClass(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	
	public void setOnDeleteListener(OnDeleteListener onDeleteListener){
		mDeleteListener=onDeleteListener;
	}

	//定義一個接口
	public interface OnDeleteListener {
		void onDelete(int index);
	}
	
	//------>以下方法為OnTouchListener接口的實現
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		//if-->當有刪除View的時候.我們觸摸該ListView時將其消失
		if (isDeleteShowing) {
			mListViewItemViewGroup.removeView(mDeleteView);
			mDeleteView = null;
			isDeleteShowing = false;
			return false;
	    //else-->其余時候交給GestureDetector處理
		} else {
			return mGestureDetector.onTouchEvent(event);
		}

	}
	//------>以下方法為OnTouchListener接口的實現
	
	

	
	//------>以下方法為OnGestureListener接口的實現
	@Override
	public boolean onDown(MotionEvent e) {
		if (!isDeleteShowing) {
			selectedItem = pointToPosition((int) e.getX(), (int) e.getY());
		}
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
		//當刪除View沒有顯示並且是水平滑動時我們顯示刪除View
		if (!isDeleteShowing && Math.abs(velocityX) > Math.abs(velocityY)) {
			mDeleteView = LayoutInflater.from(getContext()).inflate(R.layout.delete, null);
			mDeleteView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mListViewItemViewGroup.removeView(mDeleteView);
					mDeleteView = null;
					isDeleteShowing = false;
					mDeleteListener.onDelete(selectedItem);
				}
			});
			//找到當前的Item
			mListViewItemViewGroup = (ViewGroup) getChildAt(selectedItem-getFirstVisiblePosition());
			RelativeLayout.LayoutParams params = new 
			RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			params.addRule(RelativeLayout.CENTER_VERTICAL);
			mListViewItemViewGroup.addView(mDeleteView, params);
			isDeleteShowing = true;
		}
		
		return false;

	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
	//------>以上方法為OnGestureListener接口的實現

}


main.xml如下:



    

delete.xml如下:



listviewitem如下:



    





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved