listview侧滑删除
2017-08-22 15:43
246 查看
侧滑listview 代码实现
布局实现
如何使用
布局实现
side_nav_bar.xml
d_delete_btn.xml
style
删除动画delete_btn_hide.xml
public class QQListView extends ListView { //用户滑动的最小距离 private int touchSlop; //是否响应滑动 private boolean isSliding; //手指按下时的x坐标 private int xDown; //手指按下时的y坐标 private int yDown; //手指移动时的x坐标 private int xMove; //手指移动时的y坐标 private int yMove; private LayoutInflater mInflater; private PopupWindow mPopupWindow; private int mPopupWindowHeight; private int mPopupWindowWidht; //删除按钮 private Button mBtnDel; //当前手指触摸的View private View mCurrentView; // 当前手指触摸的位置 private int mCurrentViewPos; //删除回调函数 private OnDelListener mListener; public interface OnDelListener { public void onClick(int position); } public void setOnDelListener(OnDelListener listener) { mListener=listener; } public QQListView(Context context) { this(context, null); } public QQListView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public QQListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mInflater=LayoutInflater.from(getContext()); touchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop(); View view=mInflater.inflate(R.layout.view_delete, null); mBtnDel=(Button) view.findViewById(R.id.id_item_btn); mPopupWindow=new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); //先调用下measure,否则拿不到宽和高 mPopupWindow.getContentView().measure(0, 0); mPopupWindowHeight=mPopupWindow.getContentView().getMeasuredHeight(); mPopupWindowWidht=mPopupWindow.getContentView().getMeasuredWidth(); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action=ev.getAction(); int x=(int) ev.getX(); int y=(int) ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: xDown=x; yDown=y; if (mPopupWindow != null && mPopupWindow.isShowing()) { dismissPopWindow(); return false; } mCurrentViewPos=pointToPosition(xDown, yDown); View view=getChildAt(mCurrentViewPos - getFirstVisiblePosition()); mCurrentView=view; break; case MotionEvent.ACTION_MOVE: xMove=x; yMove=y; int dx=xMove - xDown; int dy=yMove - yDown; //判断是否是从右到左的滑动 if (xMove < xDown && Math.abs(dx) > touchSlop && Math.abs(y) < touchSlop) { isSliding=true; } break; } return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { int action=ev.getAction(); if (isSliding) { switch (action) { case MotionEvent.ACTION_MOVE: int[] location=new int[2]; // 获得当前item的位置x与y mCurrentView.getLocationOnScreen(location); // 设置popupWindow的动画 mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style); mPopupWindow.update(); mPopupWindow.showAtLocation(mCurrentView, Gravity.LEFT | Gravity.TOP , location[0] + mCurrentView.getWidth() , location[1] + mCurrentView.getHeight() / 2 - mPopupWindowHeight / 2); mBtnDel.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (mListener != null){ mListener.onClick(mCurrentViewPos); mPopupWindow.dismiss(); } } }); break; case MotionEvent.ACTION_UP: isSliding=false; break; } // 相应滑动期间屏幕itemClick事件,避免发生冲突 return true; } return super.onTouchEvent(ev); } /** * 隐藏popupWindow */ public void dismissPopWindow() { if (mPopupWindow != null && mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } } }
布局实现
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/id_item_btn" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="@drawable/side_nav_bar" android:paddingLeft="15dp" android:paddingRight="15dp" android:singleLine="true" android:text="删除" android:textColor="#ffffff" /> </LinearLayout>
如何使用
public class QQActivtiy extends Activity { private QQListView mListView; private ArrayAdapter<String> mAdapter; private List<String> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qq); mListView = (QQListView) findViewById(R.id.id_listview); // 不要直接Arrays.asList mDatas = new ArrayList<String>(Arrays.asList("HelloWorld", "Welcome", "Java", "Android", "Servlet", "Struts", "Hibernate", "Spring", "HTML5", "Javascript", "Lucene")); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter); mListView.setOnDelListener(new QQListView.OnDelListener() { @Override public void onClick(int position) { Toast.makeText(QQActivtiy.this, position + " : " + mAdapter.getItem(position), 1).show(); mAdapter.remove(mAdapter.getItem(position)); } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(QQActivtiy.this, position + " : " + mAdapter.getItem(position), 1).show(); } }); } }
布局实现
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.android.rxjava.Drag.QQListView android:id="@+id/id_listview" android:layout_width="fill_parent" android:layout_height="wrap_content"> </com.android.rxjava.Drag.QQListView> </LinearLayout>
side_nav_bar.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:angle="135" android:centerColor="#4CAF50" android:endColor="#2E7D32" android:startColor="#81C784" android:type="linear" /> </shape>
d_delete_btn.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_style_five_focused" android:state_focused="true"></item> <item android:drawable="@drawable/btn_style_five_pressed" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_style_five_normal"></item> </selector>
style
<style name="popwindow_delete_btn_anim_style"> <item name="android:windowEnterAnimation">@anim/delete_btn_show</item> <!-- 指定显示的动画xml --> <item name="android:windowExitAnimation">@anim/delete_btn_hide</item> <!-- 指定消失的动画xml --> </style>
删除动画delete_btn_hide.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="200" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="100%" android:pivotY="50%" android:toXScale="0.0" android:toYScale="1.0"> </scale> </set>
相关文章推荐
- Android ListView侧滑item,仿QQ删除效果
- android 仿微信侧滑删除SwipeListView实例
- 仿QQ侧滑删除,自定义ListView
- Listview侧滑删除思路浅析
- 利用 SwipeDismissListViewTouchListener 实现左右侧滑删除条目 item
- ListView侧滑删除的实现,SlideDeleteListView,针对ScrollView嵌套ListView视图和手势冲突优化
- Android侧滑删除另一种实现,SwipeListView补充
- 仿QQ侧滑删除,Listview上下滑动,Listview的iteam的点击事件等bug的解决
- expandableListview实现侧滑删除
- 开源控件学习之-侧滑listview进行删除(一)(自定义listview)
- ListView侧滑删除(SwipeMenuListView)
- android--------ListView和ExpandableListView的侧滑删除操作
- Android 模仿QQ侧滑删除ListView功能示例
- 仿QQ消息Listview侧滑置顶、删除
- Android listview 侧滑 SwipeListView 详解 实现微信,QQ等滑动删除效果
- Android listView item侧滑实现删除和置顶功能
- 仿QQ侧滑删除Item:Swipemenulistview的简单实现
- Androidlistview 的侧滑删除仿的QQ效果的几种实现方式
- Android 侧滑删除 SwipeMenuListView
- 仿QQ侧滑删除ListView