您的位置:首页 > 移动开发 > Android开发

Android 仿腾讯QQ 的 ListView滑动删除

2016-07-20 23:10 441 查看
1、代码实现

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();
}
}
}


2、 删除按钮 布局:

<?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>


3、在主界面封装数据并显示

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();
}
});
}
}


4、主布局文件

<?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>


5、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>


6、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>


7、 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>


8、删除动画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>


———————————————————————

(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)

有需求者请进站查看,非诚勿扰

https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084hc8wek&id=555888526201


01.高级架构师四十二个阶段高

02.Java高级系统培训架构课程148课时

03.Java高级互联网架构师课程

04.Java互联网架构Netty、Nio、Mina等-视频教程

05.Java高级架构设计2016整理-视频教程

06.架构师基础、高级片

07.Java架构师必修linux运维系列课程

08.Java高级系统培训架构课程116课时

(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)

——————————————————————–
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android listview 腾讯