仿QQ侧滑删除的ListView

来源:互联网 发布:假面骑士drive知乎 编辑:程序博客网 时间:2024/05/01 19:22

参照博客:鸿洋仿QQ的ListView

一、原理

①、重写ListView的点击事件,在不修改ListView的点击事件上,增加左右滑动的事件判断。
②、当判断为左右滑动的时候,在点击的Item的右侧出现PopupWindow,该Window里面是一个Button
③、设定当点击删除时候的回调事件。

二、我的问题

①、如何使用PopupWindow
②、如何获取当前手指位置的Item对象。

三、PopupWindow的使用详解

①、作用
1、显示在Activity之上  2、可以在屏幕上任意摆放。 3、可以将任意的View作为其内容
②、创建
方法名称:
public PopupWindow(View contentView, int width, int height, boolean focusable)
contentView:表示PopupWindow填充的内容。
使用(使用Layout布局给PopupWindow填充):
<span style="font-size:18px;">//通过Inflater获取layout布局的ViewView v = LayoutInflater.from(context).inflate(R.layout.xxx);PopupWindow popupWindow = new PopupWindow(v,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)</span>

width/height:表示PopupWidnow的大小。可以用LayoutParams.WRAP_CONTENT、LayoutParams.MATHC_PARENT表示。

facousable:表示是否获取焦点
一般的作用:
如果选择获取焦点,当使用手机上的back按钮时,会先销毁PopupWindow。
如果没有获取焦点,则跟Activity一起消失。
如果PopupWindow的View中有EditText的话,一定要获取焦点EditText才能被使用。

③、如何添加动画
添加方法:
public void setAnimationStyle(int animationStyle)
使用:
首先该方法说明是添加style,所以我们需要在value/style.xml中创建style
    <style name="PopupWindowAnimation">
<span style="white-space:pre"></span><!--设置入场动画-->        <item name="android:windowEnterAnimation">@anim/pop_enter</item>        <!--设置出场动画-->
<span style="white-space:pre"></span><item name="android:windowExitAnimation">@anim/pop_exit</item>    </style>
然后调用:
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
然后再调用刷新方法
popupWindow.update();

④、如何获取宽高
如果是我们设定了固定的宽高,那就没什么问题,我们直接将宽高设为常量就好了。但是如果是设定LayoutParams.WRAP_COTENT
我们怎么获取PopupWindow的宽高呢
<span style="font-size:18px;">popupWindow.getContentView().measure(0,0);int width = popupWindow.getContentView().getMeasureWidth();int height = popupWindow.getContentView().getMeasureHeight();</span>
如果直接使用
popupWindow.getWidth()/getHeight()是拿不到宽高的。

④、如何显示
方法:
public void showAtLocation(View parent, int gravity, int x, int y)
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)

第一个方法:
View parent:其实是为了获取View中 IBinder token,所以随便传一个View进去就可以了。
gravity:相对参照物。 用Grivity.LEFT/Grivity.TOP 等
Grivity.LEFT:表示x相对参照物左边偏移量。
Grivity.RIGHT:表示x相对右边的偏移量
int x,y:表示左上角的位置。

第二个方法:
作用:围绕某个View,以向下弹出的方式显示
View anchor:围绕的View
int gravitiy:同上
int xoff:设局距离View的X的距离
int yooff:设置距离View的Y的距离

⑤、如何销毁
public void dissmiss();

四、如何获取当前手指所指的Item的对象
①、调用listView.pointToPosition(int x,int y):获取该位置下的item在ListVIew中的poistion
②、获取当前第一个显示的item的position
③。两个相减就能得到,item在当前VIew中的位置(因为ViewGroup值存储当前显示的Item的对象)
④、通过getChildAT(position)获取item对象。

五。开始制作
1、设置PopupWindow的内如布局
2、初始化


0 0