国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)QQ側(cè)滑(刪除、置頂?shù)?功能

2019-12-12 01:34:07
字體:
供稿:網(wǎng)友

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

一、效果圖

二、代碼片段

主頁布局和主頁的Java代碼都和平時(shí)使用沒有區(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,里面是兩個(gè)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è)置滑動(dòng)是否可用,默認(rèn)為true  swipeLayout.setSwipeEnabled(true);  /**   * 打開時(shí)調(diào)用:循環(huán)調(diào)用onStartOpen,onUpdate,onHandRelease,onUpdate,onOpen,   * 關(guān)閉時(shí)調(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è)滑以外部分)點(diǎn)擊時(shí),可收回側(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;  } }}

說明:和平時(shí)我們寫的適配器不一樣的是繼承自BaseSwipeAdapter,需要實(shí)現(xiàn)的方法除了圖中展示的,還有一個(gè)getItemId();再?zèng)]有別的。這里主要解釋下幾個(gè)平時(shí)沒有見過的方法:

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、如果我們的這個(gè)適配器是重用的,而有些時(shí)候不需要滑動(dòng)功能,那么可以調(diào)用此方法來控制滑動(dòng)是否可用。 

swipeLayout.setSwipeEnabled(true);

2、當(dāng)我們的側(cè)邊布局還出來的時(shí)候,此時(shí)點(diǎn)擊該條item,默認(rèn)是不會(huì)收回的,也就是下面代碼默認(rèn)是false。

falseswipeLayout.setClickToClose(true);

3、如演示,當(dāng)點(diǎn)擊了刪除或者打開后,劃出來的側(cè)邊布局自動(dòng)收回了,及時(shí)通過下面的屬性closeAllItems()方法控制的。默認(rèn)是不會(huì)收回的。

this.closeAllItems();

4、前面已經(jīng)提到了,我們的側(cè)滑出現(xiàn)的位置,如有需求是需要左邊或者右邊,別忘了它:

app:drag_edge="right"

*四、使用*

compile 'com.daimajia.swipelayout:library:1.2.0'

五、總結(jié)

demo已上傳至github,鏈接放在了公眾號(hào)”code小生”里,關(guān)注查看。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 公主岭市| 东乡县| 玉田县| 金乡县| 公主岭市| 惠安县| 苗栗县| 高邑县| 抚松县| 霍林郭勒市| 承德市| 黄山市| 赞皇县| 灵山县| 奉化市| 神木县| 京山县| 清徐县| 江永县| 岢岚县| 兴文县| 朝阳县| 北流市| 腾冲县| 武乡县| 淄博市| 万山特区| 萝北县| 新乡市| 金沙县| 天气| 运城市| 会昌县| 襄樊市| 岱山县| 华安县| 双柏县| 德惠市| 莱西市| 东乌| 惠州市|