Android實現(xiàn)QQ側(cè)滑(刪除、置頂?shù)?功能
實現(xiàn)類似QQ滑動出現(xiàn)可操作項的功能,在網(wǎng)上看到有人自定義LinearLayout實現(xiàn)這個效果,但是靈活性有限。此demo使用開源項目SwipeLayout實現(xiàn)該功能。關(guān)于SwipeLayout的常用設(shè)置和屬性,這里都做介紹,下面進(jìn)入正題。
一、效果圖

二、代碼片段
主頁布局和主頁的Java代碼都和平時使用沒有區(qū)別,代碼沒必要貼出來了。這里使用的ListView演示,還可以是GridView,ExpandableListView。
最關(guān)鍵的代碼部分,ListView適配器布局:
<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:drag_edge="right">
<LinearLayout
android:id="@+id/trash"
android:layout_width="160dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:tag="Bottom">
<TextView
android:id="@+id/swipe_open"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f55509"
android:gravity="center"
android:text="打開"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/swipe_delete"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="刪除"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingLeft="5dp">
<TextView
android:id="@+id/tv_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
說明:最外層是我們的SwipeLayout,里面是兩個LinearLayout,第一層是我們的頁面布局,第二層是我們的側(cè)邊劃出來的布局。關(guān)鍵的屬性這里有體現(xiàn):
app:drag_edge="right"
此屬性是設(shè)置我們的側(cè)邊布局劃出位置,默認(rèn)是右邊,可以設(shè)置左邊、底部、頂部。
適配器Java代碼:
package com.example.mjj.swipelayoutdemo;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import java.util.List;
/**
* Description:適配器
* <p>
* Created by Mjj on 2016/12/12.
*/
public class MySwipeAdapter extends BaseSwipeAdapter implements View.OnClickListener {
private Context context;
private List<ItemBean> list;
private final String TAG = "MySwipeAdapter";
public MySwipeAdapter(Context context, List<ItemBean> list) {
this.context = context;
this.list = list;
}
/**
* 返回SwipeLayout的ID
*
* @param position
* @return
*/
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
/**
* 綁定布局
*
* @param position
* @param parent
* @return
*/
@Override
public View generateView(int position, ViewGroup parent) {
View itemView = View.inflate(context, R.layout.item_swipe, null);
SwipeLayout swipeLayout = (SwipeLayout) itemView.findViewById(getSwipeLayoutResourceId(position));
// 設(shè)置滑動是否可用,默認(rèn)為true
swipeLayout.setSwipeEnabled(true);
/**
* 打開時調(diào)用:循環(huán)調(diào)用onStartOpen,onUpdate,onHandRelease,onUpdate,onOpen,
* 關(guān)閉時調(diào)用:onStartClose,onUpdate,onHandRelease,onHandRelease,onUpdate,onClose
*/
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onStartOpen(SwipeLayout layout) {
// Log.e(TAG, "onStartOpen: ");
}
@Override
public void onOpen(SwipeLayout layout) {
// Log.e(TAG, "onOpen: ");
}
@Override
public void onStartClose(SwipeLayout layout) {
// Log.e(TAG, "onStartClose: ");
}
@Override
public void onClose(SwipeLayout layout) {
// Log.e(TAG, "onClose: ");
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
// Log.e(TAG, "onUpdate: ");
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
// Log.e(TAG, "onHandRelease: ");
}
});
// 設(shè)置為true,在當(dāng)前一條item(除側(cè)滑以外部分)點擊時,可收回側(cè)滑出來部分,默認(rèn)為false
swipeLayout.setClickToClose(true);
// SwipeLayout單擊事件,可替代ListView的OnitemClickListener事件.
swipeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Log.e(TAG, "onClick: ");
}
});
return itemView;
}
/**
* 綁定數(shù)據(jù)
*
* @param position
* @param convertView
*/
@Override
public void fillValues(int position, View convertView) {
TextView tvNickName = (TextView) convertView.findViewById(R.id.tv_nickname);
TextView tvMsg = (TextView) convertView.findViewById(R.id.tv_msg);
TextView tvSwipeOpen = (TextView) convertView.findViewById(R.id.swipe_open);
TextView tvSwipeDelete = (TextView) convertView.findViewById(R.id.swipe_delete);
tvNickName.setText(list.get(position).getNickName());
tvMsg.setText(list.get(position).getMsg());
tvSwipeDelete.setOnClickListener(this);
tvSwipeOpen.setOnClickListener(this);
}
@Override
public int getCount() {
return list.size();
}
@Override
public ItemBean getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.swipe_open:
// 關(guān)閉所有打開的Swipe的item
this.closeAllItems();
Toast.makeText(context, "Swipe--Open", Toast.LENGTH_SHORT).show();
break;
case R.id.swipe_delete:
this.closeAllItems();
Toast.makeText(context, "Swipe--Delete", Toast.LENGTH_SHORT).show();
break;
}
}
}
說明:和平時我們寫的適配器不一樣的是繼承自BaseSwipeAdapter,需要實現(xiàn)的方法除了圖中展示的,還有一個getItemId();再沒有別的。這里主要解釋下幾個平時沒有見過的方法:
public int getSwipeLayoutResourceId(int position)
此方法返回的是我們的SwipeLayout的ID,而不是布局的ID。
public View generateView(int position, ViewGroup parent)
此方法返回的作用是和我們的item布局進(jìn)行關(guān)聯(lián)的,并在這里設(shè)置swipeLayout的相關(guān)屬性。
public void fillValues(int position, View convertView)
此方法用來給我們的item中的控件綁定數(shù)據(jù),并根據(jù)需要設(shè)置事件等操作。
*三、常用設(shè)置介紹*
1、如果我們的這個適配器是重用的,而有些時候不需要滑動功能,那么可以調(diào)用此方法來控制滑動是否可用。
swipeLayout.setSwipeEnabled(true);
2、當(dāng)我們的側(cè)邊布局還出來的時候,此時點擊該條item,默認(rèn)是不會收回的,也就是下面代碼默認(rèn)是false。
falseswipeLayout.setClickToClose(true);
3、如演示,當(dāng)點擊了刪除或者打開后,劃出來的側(cè)邊布局自動收回了,及時通過下面的屬性closeAllItems()方法控制的。默認(rèn)是不會收回的。
this.closeAllItems();
4、前面已經(jīng)提到了,我們的側(cè)滑出現(xiàn)的位置,如有需求是需要左邊或者右邊,別忘了它:
app:drag_edge="right"
*四、使用*
compile 'com.daimajia.swipelayout:library:1.2.0'
五、總結(jié)
demo已上傳至github,鏈接放在了公眾號”code小生”里,關(guān)注查看。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)圖片點擊預(yù)覽效果(zoom動畫)
本文主要介紹了Android實現(xiàn)圖片點擊預(yù)覽效果的方法步驟。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03
Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法
下面小編就為大家分享一篇Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android+Flutter實現(xiàn)文字跑馬燈特效
跑馬燈常常被運用在很多領(lǐng)域, 例如商場的電子條幅、大樓的宣傳廣告位、地鐵的廣告位。今天我們來說一下flutter 怎么通過繪制來實現(xiàn)跑馬燈效果!,希望對大家有所幫助2022-11-11
android基礎(chǔ)總結(jié)篇之一:Activity生命周期
本篇文章主要介紹了android基礎(chǔ)總結(jié)篇之一:Activity生命周期,想要學(xué)習(xí)的可以了解一下。2016-11-11
Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中ToggleButton開關(guān)狀態(tài)按鈕控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android實戰(zhàn)打飛機(jī)游戲之菜單頁面設(shè)計(1)
這篇文章主要為大家詳細(xì)介紹了Android實戰(zhàn)打飛機(jī)游戲之菜單頁面設(shè)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android從系統(tǒng)Gallery獲取圖片具體實現(xiàn)
這篇文章主要介紹了Android從系統(tǒng)Gallery獲取圖片具體實現(xiàn),有需要的朋友可以參考一下2013-12-12
Android在一個app中安裝并卸載另一個app的示例代碼
這篇文章主要介紹了Android在一個app中安裝并卸載另一個app的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Android自定義View實現(xiàn)繪制虛線的方法詳解
這篇文章主要給大家介紹了Android自定義View實現(xiàn)繪制虛線的方法,在繪制過程中走了一些彎路才實現(xiàn)了虛線的效果,所以想著總結(jié)分享出來,方便有需要的朋友和自己在需要的時候參考學(xué)習(xí),下面來一起看看吧。2017-04-04

