packagecom.qudoulicai.www.qqlistview.QQlistview;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.util.DisplayMetrics;importandroid.view.MotionEvent;importandroid.view.ViewGroup;importandroid.view.WindowManager;importandroid.widget.LinearLayout;importandroid.widget.ListView;publicclassQQlistviewextendsListView{privateintmScreenWidth;//屏幕宽度privateintmDownX;//按下点的x值privateintmDownY;//按下点的y值privateintmDeleteBtnWidth;//删除按钮的宽度privatebooleanisDeleteShown;//删除按钮是否正在显示privateViewGroupmPointChild;//当前处理的itemprivateLinearLayout.LayoutParamsmLayoutParams;//当前处理的item的LayoutParamspublicQQlistview(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicQQlistview(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);//获取屏幕宽度WindowManagerwm=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);DisplayMetricsdm=newDisplayMetrics();wm.getDefaultDisplay().getMetrics(dm);mScreenWidth=dm.widthPixels;}@OverridepublicbooleanonTouchEvent(MotionEventev){switch(ev.getAction()){caseMotionEvent.ACTION_DOWN:performActionDown(ev);break;caseMotionEvent.ACTION_MOVE:returnperformActionMove(ev);caseMotionEvent.ACTION_UP:performActionUp();break;}returnsuper.onTouchEvent(ev);}//处理action_down事件privatevoidperformActionDown(MotionEventev){if(isDeleteShown){turnToNormal();}mDownX=(int)ev.getX();mDownY=(int)ev.getY();//获取当前点的itemmPointChild=(ViewGroup)getChildAt(pointToPosition(mDownX,mDownY)-getFirstVisiblePosition());//获取删除按钮的宽度mDeleteBtnWidth=mPointChild.getChildAt(1).getLayoutParams().width;mLayoutParams=(LinearLayout.LayoutParams)mPointChild.getChildAt(0).getLayoutParams();//为什么要重新设置layout_width等于屏幕宽度//因为match_parent时,不管你怎么滑,都不会显示删除按钮//why?因为match_parent时,ViewGroup就不去布局剩下的viewmLayoutParams.width=mScreenWidth;mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);}//处理action_move事件privatebooleanperformActionMove(MotionEventev){intnowX=(int)ev.getX();intnowY=(int)ev.getY();if(Math.abs(nowX-mDownX)>Math.abs(nowY-mDownY)){//如果向左滑动if(nowX<mDownX){//计算要偏移的距离intscroll=(nowX-mDownX)/2;//如果大于了删除按钮的宽度,则最大为删除按钮的宽度if(-scroll>=mDeleteBtnWidth){scroll=-mDeleteBtnWidth;}//重新设置leftMarginmLayoutParams.leftMargin=scroll+10;mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);}returntrue;}returnsuper.onTouchEvent(ev);}//处理action_up事件privatevoidperformActionUp(){//偏移量大于button的一半,则显示button//否则恢复默认if(-mLayoutParams.leftMargin>=mDeleteBtnWidth/2){mLayoutParams.leftMargin=-mDeleteBtnWidth;isDeleteShown=true;}else{turnToNormal();}mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);}/***变为正常状态*/publicvoidturnToNormal(){mLayoutParams.leftMargin=0;mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);isDeleteShown=false;}/***当前是否可点击*@return是否可点击*/publicbooleancanClick(){return!isDeleteShown;}}//非原创原创地址太偏。。搬运过来


更多相关文章

  1. android 实现透明按钮
  2. Android(安卓)点击回退按钮时弹出提示框
  3. Android(安卓)点击按钮隐藏键盘
  4. beforeTextChanged,TextChanged,afterTextChanged的使用Android
  5. Android设置RadioButton在文字的右边
  6. android 根据TextView宽度 自动缩小字体大小 以适应其宽度
  7. Win8风格的按钮、ToggleButton、SeekBar
  8. 自动更新版本
  9. 【Android】编写一个简易计算机

随机推荐

  1. 如何将 Android(安卓)仿真器的速度提升 4
  2. android things 树莓派B3 GPIO口测试及问
  3. Android----Thread+Handler 线程 消息循
  4. Android层接收串口数据
  5. android 输入法(包括手写界面)
  6. Android(安卓)系统 root 破解原理分析
  7. Android(安卓)双击返回键退出程序的方法
  8. Android程序签名
  9. Android(安卓)SQlite数据库常规操作
  10. postDelayed方法和removeCallbacks方法的