Android仿騰訊QQ實現(xiàn)滑動刪除 附源碼下載
看了很多大神們的文章,感覺受益良多,也非常欣賞大家的分享態(tài)度,所以決定開始寫B(tài)log,給大家分享自己的心得。
先看看效果圖:

本來準(zhǔn)備在ListView的每個Item的布局上設(shè)置一個隱藏的Button,當(dāng)滑動的時候顯示。但是因為每次只要存在一個Button,發(fā)現(xiàn)每個Item上的Button相互間不好控制。所以決定繼承ListView然后結(jié)合PopupWindow。
首先是布局文件:
delete_btn.xml:這里只需要一個Button
<?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:singleLine="true" android:layout_height="wrap_content" android:text="刪除" android:background="@drawable/d_delete_btn" android:textColor="#ffffff" android:paddingLeft="15dp" android:paddingRight="15dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" /> </LinearLayout>
主布局文件:activity_main.xml,ListView的每個Item的樣式直接使用了系統(tǒng)的android.R.layout.simple_list_item_1
<RelativeLayout xmlns: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.listviewitemslidedeletebtnshow.QQListView android:id="@+id/id_listview" android:layout_width="fill_parent" android:layout_height="wrap_content" > </com.example.listviewitemslidedeletebtnshow.QQListView> </RelativeLayout>
接下來看看QQListView的實現(xiàn):
package com.example.listviewitemslidedeletebtnshow;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
public class QQListView extends ListView
{
private static final String TAG = "QQlistView";
// private static final int VELOCITY_SANP = 200;
// private VelocityTracker mVelocityTracker;
/**
* 用戶滑動的最小距離
*/
private int touchSlop;
/**
* 是否響應(yīng)滑動
*/
private boolean isSliding;
/**
* 手指按下時的x坐標(biāo)
*/
private int xDown;
/**
* 手指按下時的y坐標(biāo)
*/
private int yDown;
/**
* 手指移動時的x坐標(biāo)
*/
private int xMove;
/**
* 手指移動時的y坐標(biāo)
*/
private int yMove;
private LayoutInflater mInflater;
private PopupWindow mPopupWindow;
private int mPopupWindowHeight;
private int mPopupWindowWidth;
private Button mDelBtn;
/**
* 為刪除按鈕提供一個回調(diào)接口
*/
private DelButtonClickListener mListener;
/**
* 當(dāng)前手指觸摸的View
*/
private View mCurrentView;
/**
* 當(dāng)前手指觸摸的位置
*/
private int mCurrentViewPos;
/**
* 必要的一些初始化
*
* @param context
* @param attrs
*/
public QQListView(Context context, AttributeSet attrs)
{
super(context, attrs);
mInflater = LayoutInflater.from(context);
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
View view = mInflater.inflate(R.layout.delete_btn, null);
mDelBtn = (Button) view.findViewById(R.id.id_item_btn);
mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
/**
* 先調(diào)用下measure,否則拿不到寬和高
*/
mPopupWindow.getContentView().measure(0, 0);
mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();
mPopupWindowWidth = 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;
/**
* 如果當(dāng)前popupWindow顯示,則直接隱藏,然后屏蔽ListView的touch事件的下傳
*/
if (mPopupWindow.isShowing())
{
dismissPopWindow();
return false;
}
// 獲得當(dāng)前手指按下時的item的位置
mCurrentViewPos = pointToPosition(xDown, yDown);
// 獲得當(dāng)前手指按下時的item
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(dy) < touchSlop)
{
// Log.e(TAG, "touchslop = " + touchSlop + " , dx = " + dx +
// " , dy = " + dy);
isSliding = true;
}
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
int action = ev.getAction();
/**
* 如果是從右到左的滑動才相應(yīng)
*/
if (isSliding)
{
switch (action)
{
case MotionEvent.ACTION_MOVE:
int[] location = new int[2];
// 獲得當(dāng)前item的位置x與y
mCurrentView.getLocationOnScreen(location);
// 設(shè)置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);
// 設(shè)置刪除按鈕的回調(diào)
mDelBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (mListener != null)
{
mListener.clickHappend(mCurrentViewPos);
mPopupWindow.dismiss();
}
}
});
// Log.e(TAG, "mPopupWindow.getHeight()=" + mPopupWindowHeight);
break;
case MotionEvent.ACTION_UP:
isSliding = false;
}
// 相應(yīng)滑動期間屏幕itemClick事件,避免發(fā)生沖突
return true;
}
return super.onTouchEvent(ev);
}
/**
* 隱藏popupWindow
*/
private void dismissPopWindow()
{
if (mPopupWindow != null && mPopupWindow.isShowing())
{
mPopupWindow.dismiss();
}
}
public void setDelButtonClickListener(DelButtonClickListener listener)
{
mListener = listener;
}
interface DelButtonClickListener
{
public void clickHappend(int position);
}
}
代碼注釋寫得很詳細(xì),簡單說一下,在dispatchTouchEvent中設(shè)置當(dāng)前是否響應(yīng)用戶滑動,然后在onTouchEvent中判斷是否響應(yīng),如果響應(yīng)則popupWindow以動畫的形式展示出來。當(dāng)然屏幕上如果存在PopupWindow則屏幕ListView的滾動與Item的點擊,以及從右到左滑動時屏幕Item的click事件。
接下來是MainActivity.java,這里代碼很簡單不做介紹了。
package com.example.listviewitemslidedeletebtnshow;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.example.listviewitemslidedeletebtnshow.QQListView.DelButtonClickListener;
public class MainActivity 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_main);
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.setDelButtonClickListener(new DelButtonClickListener()
{
@Override
public void clickHappend(final int position)
{
Toast.makeText(MainActivity.this, position + " : " + mAdapter.getItem(position), 1).show();
mAdapter.remove(mAdapter.getItem(position));
}
});
mListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(MainActivity.this, position + " : " + mAdapter.getItem(position), 1).show();
}
});
}
}
樓主使用asm.jar以及gifcamera截的gif,由于button的動畫很短感覺截圖效果很卡不流暢,大家有什么好的截圖,還望推薦。
有興趣的還是下載源碼看看效果,源碼下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?Flutter實現(xiàn)任意拖動的控件
使用flutter開發(fā)是需要控件能拖動,比如畫板中的元素,或者工具條等,所以本文為大家準(zhǔn)備了Flutter實現(xiàn)任意拖動控件的示例代碼,希望對大家有所幫助2023-07-07
探討Android 的屏幕滾動操作不如 iPhone 流暢順滑的原因
雖然很多Android手機的配置都比iPhone要高,比如大多數(shù)Andorid手機的內(nèi)存都有1GB,而iPhone 4S只有512MB內(nèi)存,但用過iPhone的人都知道Android手機在使用的時候總感覺沒有那么順滑,究竟為什么會出現(xiàn)這種現(xiàn)象呢?2014-07-07
SwipeLayout框架實現(xiàn)側(cè)拉刪除編輯功能
這篇文章主要為大家詳細(xì)介紹了SwipeLayout框架實現(xiàn)側(cè)拉刪除編輯功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08

