編輯:關於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如下:
最近的項目中遇到一個非常頭痛的需求,在Android端錄制視頻的時候動態添加像監控畫面一樣的精確到秒的時間信息,關鍵是,並不是說只在播放器的界面顯示時間就可以了,而是錄制
前面的博客中,我給大家分析過數組和鏈表兩種線性表數據結構。數組存儲區間連續,查找方便,但是插入和刪除效率低下;鏈表存儲區間離散,插入刪除方便,但是查找困難。大家肯定會問,
現如今,驗證碼在Android的客戶端還是非常普遍的.通過手機賬號和驗證碼直接去注冊應用賬戶的信息.很多應用都以這種方式來完成注冊.簡單的介紹一下吧.&nbs
一、 看效果二、上代碼package com.framework.widget;import android.app.Activity;import android.co