本案例主要实现了ListView和ExpandableListView的侧滑删除操作功能
效果图:
代码:
1 自定义ListView 2 public class ZQListView extends ListView { 3 4 private static final String TAG ="ZQListView "; 5 6 private ZQView mFocusedItemView; 7 8 float mX = 0; 9 float mY = 0;10 private int mPosition = -1;11 boolean isSlider = false;12 13 public ZQListView(Contextcontext) {14 super(context);15 }16 17 public ZQListView(Contextcontext, AttributeSet attrs) {18 super(context,attrs);19 }20 21 public ZQListView(Contextcontext, AttributeSet attrs,int defStyle) {22 super(context,attrs, defStyle);23 }24 25 @Override26 public boolean onTouchEvent(MotionEvent event) {27 float x =event.getX();28 float y =event.getY();29 switch(event.getAction()) {30 case MotionEvent.ACTION_DOWN:31 isSlider = false;32 mX = x;33 mY = y;34 int position= pointToPosition((int) x, (int) y);35 if (mPosition !=position) {36 mPosition =position;37 if (mFocusedItemView !=null) {38 mFocusedItemView.reset();39 }40 }41 break;42 case MotionEvent.ACTION_MOVE:43 if (mPosition != -1) {44 if (Math.abs(mY - y) <30 && Math.abs(mX - x) > 20) {45 int first =this.getFirstVisiblePosition();46 int index =mPosition - first;47 mFocusedItemView =(ZQView) getChildAt(index);48 mFocusedItemView.onTouchEvent(event);49 isSlider = true;50 return true;51 }52 }53 break;54 case MotionEvent.ACTION_UP:55 if (isSlider) {56 isSlider = false;57 if (mFocusedItemView !=null) {58 mFocusedItemView.adjust(mX - x >0);59 return true;60 }61 }62 break;63 }64 return super.onTouchEvent(event);65 }66 }
1 侧滑布局类 2 public class ZQView extends LinearLayout { 3 private static final String TAG ="SlideView"; 4 private static final int TAN = 2; 5 private int mHolderWidth = 120; 6 private float mLastX = 0; 7 private float mLastY = 0; 8 private Context mContext; 9 private LinearLayout mViewContent; 10 private Scroller mScroller; 11 12 public ZQView(Contextcontext, Resources resources) { 13 super(context); 14 initView(); 15 } 16 17 public ZQView(Contextcontext) { 18 super(context); 19 initView(); 20 } 21 22 public ZQView(Contextcontext, AttributeSet attrs) { 23 super(context,attrs); 24 initView(); 25 } 26 27 private void initView() { 28 setOrientation(LinearLayout.HORIZONTAL); 29 mContext = getContext(); 30 mScroller = new Scroller(mContext); 31 View.inflate(mContext,R.layout.delete_view,this); 32 mViewContent = (LinearLayout)findViewById(R.id.view_content); 33 mHolderWidth = Math.round(TypedValue.applyDimension( 34 TypedValue.COMPLEX_UNIT_DIP,mHolderWidth,getResources() 35 .getDisplayMetrics())); 36 } 37 38 public void setContentView(View view) { 39 mViewContent.addView(view); 40 } 41 42 public void shrink(){ 43 int offset =getScrollX(); 44 if (offset== 0) { 45 return; 46 } 47 scrollTo(0, 0); 48 } 49 50 public void reset() { 51 int offset =getScrollX(); 52 if (offset== 0) { 53 return; 54 } 55 smoothScrollTo(0, 0); 56 } 57 58 public void adjust(boolean left) { 59 int offset =getScrollX(); 60 if (offset== 0) { 61 return; 62 } 63 if (offset< 20) { 64 this.smoothScrollTo(0,0); 65 } else if (offset< mHolderWidth - 20) { 66 if (left) { 67 this.smoothScrollTo(mHolderWidth, 0); 68 } else { 69 this.smoothScrollTo(0,0); 70 } 71 } else { 72 this.smoothScrollTo(mHolderWidth, 0); 73 } 74 } 75 76 @Override 77 public boolean onTouchEvent(MotionEvent event) { 78 switch(event.getAction()) { 79 case MotionEvent.ACTION_MOVE: 80 float x =event.getX(); 81 float y =event.getY(); 82 float deltaX =x -mLastX; 83 float delatY =y -mLastY; 84 mLastX = x; 85 mLastY = y; 86 if (Math.abs(deltaX)< Math.abs(delatY) *TAN) { 87 break; 88 } 89 if (deltaX!= 0) { 90 float newScrollX = getScrollX() - deltaX; 91 if(newScrollX < 0) { 92 newScrollX = 0; 93 } else if(newScrollX > mHolderWidth) { 94 newScrollX = mHolderWidth; 95 } 96 this.scrollTo((int)newScrollX, 0); 97 } 98 break; 99 }100 return super.onTouchEvent(event);101 }102 103 private void smoothScrollTo(int destX,int destY) {104 int scrollX =getScrollX();105 int delta =destX - scrollX;106 mScroller.startScroll(scrollX,0, delta, 0, Math.abs(delta) * 3);107 invalidate();108 }109 110 @Override111 public void computeScroll() {112 if (mScroller.computeScrollOffset()){113 scrollTo(mScroller.getCurrX(),mScroller.getCurrY());114 postInvalidate();115 }116 }
delete.xml<?xml version="1.0" encoding="utf-8"?><merge xmlns:Android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/view_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> </LinearLayout> <RelativeLayout android:id="@+id/holder" android:layout_width="120dp" android:layout_height="match_parent" android:background="@drawable/holder_bg" android:clickable="true"> <TextView android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:drawableLeft="@drawable/del_icon_normal" android:gravity="center" android:text="删除" android:textColor="@color/floralwhite"/> </RelativeLayout> </merge>listview.xml<LinearLayoutxmlns: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"> <com.example.util.ZQListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff4f7f9" android:cacheColorHint="#00000000" android:divider="#6c6c6c" android:dividerHeight="1dp" android:drawSelectorOnTop="false" android:listSelector="#00000000" android:scrollbars="none" android:scrollingCache="false"/> </LinearLayout>
1 Activity和适配器类 2 public class ActivityListViewDelete extends Activity implements 3 OnItemClickListener, OnClickListener { 4 5 private ZQListView mListView; 6 7 private List<MessageItem> mMessageItems = new ArrayList<ActivityListViewDelete.MessageItem>(); 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 initView(); 14 } 15 16 public static int[] Img = {R.drawable.ssdk_oks_classic_qq, 17 R.drawable.ssdk_oks_classic_qzone, 18 R.drawable.ssdk_oks_classic_shortmessage, 19 R.drawable.ssdk_oks_classic_sinaweibo, 20 R.drawable.ssdk_oks_classic_tencentweibo, 21 R.drawable.ssdk_oks_classic_vkontakte, 22 R.drawable.ssdk_oks_classic_wechat, 23 R.drawable.ssdk_oks_classic_wechatfavorite, 24 R.drawable.ssdk_oks_classic_wechatmoments, 25 R.drawable.ssdk_oks_classic_yixinmoments }; 26 27 private void initView() { 28 mListView = (ZQListView)findViewById(R.id.list); 29 int length = Img.length; 30 for (int i = 0; i< length; i++) { 31 MessageItem item = new MessageItem(); 32 item.iconRes = Img[i]; 33 item.title = "标题" + (i + 1); 34 item.msg = "消息" + (i + 1); 35 item.time = "2016-6-1" + i; 36 mMessageItems.add(item); 37 } 38 mListView.setAdapter(newSlideAdapter()); 39 mListView.setOnItemClickListener(this); 40 } 41 42 private class SlideAdapter extends BaseAdapter { 43 44 private LayoutInflater mInflater; 45 46 SlideAdapter() { 47 super(); 48 mInflater =getLayoutInflater(); 49 } 50 51 @Override 52 public int getCount() { 53 return mMessageItems.size(); 54 } 55 56 @Override 57 public Object getItem(int position) { 58 return mMessageItems.get(position); 59 } 60 61 @Override 62 public long getItemId(int position) { 63 return position; 64 } 65 66 @Override 67 public View getView(int position, View convertView, ViewGroupparent) { 68 ViewHolder holder; 69 ZQView slideView = (ZQView)convertView; 70 if(slideView == null) { 71 View itemView = mInflater.inflate(R.layout.list_item,null); 72 73 slideView = newZQView(ActivityListViewDelete.this); 74 slideView.setContentView(itemView); 75 holder = newViewHolder(slideView); 76 slideView.setTag(holder); 77 } else { 78 holder = (ViewHolder) slideView.getTag(); 79 } 80 MessageItem item = mMessageItems.get(position); 81 slideView.shrink(); 82 holder.icon.setImageResource(item.iconRes); 83 holder.title.setText(item.title); 84 holder.time.setText(item.time); 85 holder.deleteHolder.setOnClickListener(ActivityListViewDelete.this); 86 87 return slideView; 88 } 89 90 } 91 92 public class MessageItem { 93 public int iconRes; 94 public String title; 95 public String msg; 96 public String time; 97 } 98 99 private static class ViewHolder {100 public ImageViewicon;101 public TextView title;102 public TextView time;103 public ViewGroup deleteHolder;104 105 ViewHolder(View view) {106 icon =(ImageView) view.findViewById(R.id.icon);107 title =(TextView) view.findViewById(R.id.title);108 time =(TextView) view.findViewById(R.id.time);109 deleteHolder =(ViewGroup) view.findViewById(R.id.holder);110 }111 }112 113 @Override114 public void onItemClick(AdapterView<?> parent, View view, int position,115 long id) {116 Toast.makeText(this,"onItemClickposition=" + position, 1).show();117 118 }119 120 @Override121 public void onClick(View v) {122 if(v.getId() == R.id.holder) {123 Toast.makeText(this,"删除操作",1).show();124 125 }126 }127 128 }
此代码是ListView的侧滑删除操作,ExpandableListView未贴出来,下载代码即可,两个都有。
源码下载:
Eclipse下载:http://download.csdn.net/detail/dickyqie/9627652
AndroidStudio下载: https://github.com/DickyQie/ListViewExpandableListViewDelete