android--SwipeListView实现ListView滑动删除效果

来源:互联网 发布:mysql长整型是什么 编辑:程序博客网 时间:2024/05/03 21:36

原文转自:http://blog.csdn.net/lmj623565791/article/details/28508769


今天看别人项目,看到别人使用了SwipeListView,Google一把,果然github上的,也参考了csdn上的几篇文章,然后自己写了个例子,分享给大家。

效果图:


嗯,看一眼SwipeListView的参数的设置:

If you decide to use SwipeListView as a view, you can define it in your xml layout like this:

    <com.fortysevendeg.swipelistview.SwipeListView            xmlns:swipe="http://schemas.android.com/apk/res-auto"            android:id="@+id/example_lv_list"            android:listSelector="#00000000"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            swipe:swipeFrontView="@+id/front"            swipe:swipeBackView="@+id/back"            swipe:swipeActionLeft="[reveal | dismiss]"            swipe:swipeActionRight="[reveal | dismiss]"            swipe:swipeMode="[none | both | right | left]"            swipe:swipeCloseAllItemsWhenMoveList="[true | false]"            swipe:swipeOpenOnLongPress="[true | false]"            swipe:swipeAnimationTime="[miliseconds]"            swipe:swipeOffsetLeft="[dimension]"            swipe:swipeOffsetRight="[dimension]"            />
  • swipeFrontView - Required - front view id. 即ListView Item正常显示的控件Id,且必须与Item的布局文件中的控件id一样
  • swipeBackView - Required - back view id.  手指滑动时显示的,隐藏在FrontView后面,且必须与item的布局文件中控件Id一样
  • swipeActionLeft - Optional - left swipe action Default: 'reveal'  左滑的动作,默认reveal,即显示BackView,还有dismiss,choice会触发响应的方法。
  • swipeActionRight - Optional - right swipe action Default: 'reveal' 同上
  • swipeMode - Gestures to enable or 'none'. Default: 'both' 设置左滑、右滑、都支持
  • swipeCloseAllItemsWhenMoveList - Close revealed items on list motion. Default: 'true' 当滚动listview时,关闭所有展开的Item,最好不要设置为false,由于item的复用,false存在一些问题。
  • swipeOpenOnLongPress - Reveal on long press Default: 'true' 长按时触发显示
  • swipeAnimationTime - item drop animation time. Default: android configuration 动画时间长度
  • swipeOffsetLeft - left offset 左偏移量
  • swipeOffsetRight - right offset 右偏移量
基本属性都介绍了,下面上例子:
1、布局文件
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/white"  
  6.     android:clickable="true"  
  7.     android:orientation="vertical" xmlns:swipe="http://schemas.android.com/apk/res/com.example.zhy_swipelistview02">  
  8.   
  9.     <include  
  10.         layout="@layout/main_title"  
  11.         android:focusable="true" />  
  12.   
  13.     <FrameLayout  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="match_parent" >  
  16.   
  17.         <com.fortysevendeg.swipelistview.SwipeListView  
  18.             android:id="@+id/id_swipelistview"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="fill_parent"  
  21.             swipe:swipeActionLeft="reveal"  
  22.             swipe:swipeBackView="@+id/id_back"  
  23.             swipe:swipeCloseAllItemsWhenMoveList="true"  
  24.             swipe:swipeFrontView="@+id/id_front"  
  25.             swipe:swipeMode="left"  
  26.             swipe:swipeOffsetLeft="200dip"  
  27.             swipe:swipeOpenOnLongPress="false" />  
  28.   
  29.         <TextView  
  30.             android:id="@+id/empty"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_gravity="center"  
  34.             android:drawableTop="@drawable/no_chat"  
  35.             android:text="木有联系人"  
  36.             android:textColor="#ffb7b7b7"  
  37.             android:textSize="14.0sp"  
  38.             android:visibility="gone" />  
  39.     </FrameLayout>  
  40.   
  41.     <requestFocus />  
  42.   
  43. </LinearLayout>  

item的布局文件:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="60dp" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/id_back"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:background="#ffcccccc"  
  11.         android:gravity="center|right" >  
  12.   
  13.         <Button  
  14.             android:id="@+id/id_remove"  
  15.             android:layout_width="60dp"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_gravity="center"  
  18.             android:layout_marginRight="4dp"  
  19.             android:background="@drawable/red_button"  
  20.             android:text="Delete"  
  21.             android:textColor="#fff" >  
  22.         </Button>  
  23.     </LinearLayout>  
  24.   
  25.     <LinearLayout  
  26.         android:id="@+id/id_front"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="match_parent"  
  29.         android:background="#ffffffff" >  
  30.   
  31.         <TextView  
  32.             android:id="@+id/id_text"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_marginLeft="10dp"  
  36.             android:gravity="center_vertical"  
  37.             android:minHeight="?android:attr/listPreferredItemHeight"  
  38.             android:textAppearance="?android:attr/textAppearanceLarge"  
  39.             android:textColor="#000"  
  40.             android:textSize="25sp" >  
  41.         </TextView>  
  42.     </LinearLayout>  
  43.   
  44. </FrameLayout>  

注意对应布局的id和swipeListView中的frontView和backView的Id一致。

2、MainActivity
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.zhy_swipelistview02;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.Window;  
  10.   
  11. import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;  
  12. import com.fortysevendeg.swipelistview.SwipeListView;  
  13.   
  14. public class MainActivity extends Activity  
  15. {  
  16.   
  17.     protected static final String TAG = "Activity";  
  18.     private SwipeListView mSwipeListView;  
  19.     private DataAdapter mAdapter;  
  20.     private List<String> mDatas;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState)  
  24.     {  
  25.         super.onCreate(savedInstanceState);  
  26.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  27.         setContentView(R.layout.activity_main);  
  28.   
  29.         initDatas();  
  30.           
  31.         mSwipeListView = (SwipeListView) findViewById(R.id.id_swipelistview);  
  32.         mAdapter = new DataAdapter(this, mDatas , mSwipeListView);  
  33.         mSwipeListView.setAdapter(mAdapter);  
  34.   
  35.         mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener()  
  36.         {  
  37.             @Override  
  38.             public void onChoiceChanged(int position, boolean selected)  
  39.             {  
  40.                 Log.d(TAG, "onChoiceChanged:" + position + ", " + selected);  
  41.             }  
  42.   
  43.             @Override  
  44.             public void onChoiceEnded()  
  45.             {  
  46.                 Log.d(TAG, "onChoiceEnded");  
  47.             }  
  48.   
  49.             @Override  
  50.             public void onChoiceStarted()  
  51.             {  
  52.                 Log.d(TAG, "onChoiceStarted");  
  53.             }  
  54.   
  55.             @Override  
  56.             public void onClickBackView(int position)  
  57.             {  
  58.                 Log.d(TAG, "onClickBackView:" + position);  
  59.             }  
  60.   
  61.             @Override  
  62.             public void onClickFrontView(int position)  
  63.             {  
  64.                 Log.d(TAG, "onClickFrontView:" + position);  
  65.             }  
  66.   
  67.             @Override  
  68.             public void onClosed(int position, boolean fromRight)  
  69.             {  
  70.                 Log.d(TAG, "onClosed:" + position + "," + fromRight);  
  71.             }  
  72.   
  73.             @Override  
  74.             public void onDismiss(int[] reverseSortedPositions)  
  75.             {  
  76.                 Log.d(TAG, "onDismiss");  
  77.               
  78.             }  
  79.   
  80.             @Override  
  81.             public void onFirstListItem()  
  82.             {  
  83.                 Log.d(TAG, "onFirstListItem");  
  84.             }  
  85.   
  86.             @Override  
  87.             public void onLastListItem()  
  88.             {  
  89.                 Log.d(TAG, "onLastListItem");  
  90.             }  
  91.   
  92.             @Override  
  93.             public void onListChanged()  
  94.             {  
  95.                 Log.d(TAG, "onListChanged");  
  96.   
  97.                 mSwipeListView.closeOpenedItems();  
  98.   
  99.             }  
  100.   
  101.             @Override  
  102.             public void onMove(int position, float x)  
  103.             {  
  104.                 Log.d(TAG, "onMove:" + position + "," + x);  
  105.             }  
  106.   
  107.             @Override  
  108.             public void onOpened(int position, boolean toRight)  
  109.             {  
  110.                 Log.d(TAG, "onOpened:" + position + "," + toRight);  
  111.             }  
  112.   
  113.             @Override  
  114.             public void onStartClose(int position, boolean right)  
  115.             {  
  116.                 Log.d(TAG, "onStartClose:" + position + "," + right);  
  117.             }  
  118.   
  119.             @Override  
  120.             public void onStartOpen(int position, int action, boolean right)  
  121.             {  
  122.                 Log.d(TAG, "onStartOpen:" + position + "," + action + ","  
  123.                         + right);  
  124.             }  
  125.         });  
  126.     }  
  127.   
  128.     private void initDatas()  
  129.     {  
  130.         mDatas = new ArrayList<String>();  
  131.         for (int i = 'A'; i <= 'Z'; i++)  
  132.             mDatas.add((char) i + "");  
  133.     }  
  134.   
  135. }  

Adapter:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.zhy_swipelistview02;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.fortysevendeg.swipelistview.SwipeListView;  
  6.   
  7. import android.content.Context;  
  8. import android.util.Log;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.ViewGroup;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.Button;  
  15. import android.widget.TextView;  
  16.   
  17. public class DataAdapter extends BaseAdapter  
  18. {  
  19.   
  20.     private List<String> mDatas;  
  21.     private LayoutInflater mInflater;  
  22.     private SwipeListView mSwipeListView ;  
  23.   
  24.     public DataAdapter(Context context, List<String> datas , SwipeListView swipeListView)  
  25.     {  
  26.         this.mDatas = datas;  
  27.         mInflater = LayoutInflater.from(context);  
  28.         mSwipeListView = swipeListView;  
  29.     }  
  30.   
  31.     @Override  
  32.     public int getCount()  
  33.     {  
  34.         return mDatas.size();  
  35.     }  
  36.   
  37.     @Override  
  38.     public Object getItem(int position)  
  39.     {  
  40.         return mDatas.get(position);  
  41.     }  
  42.   
  43.     @Override  
  44.     public long getItemId(int position)  
  45.     {  
  46.         return position;  
  47.     }  
  48.   
  49.     @Override  
  50.     public View getView(final int position, View convertView, ViewGroup parent)  
  51.     {  
  52.         convertView = mInflater.inflate(R.layout.list_item, null);  
  53.   
  54.         TextView tv = (TextView) convertView.findViewById(R.id.id_text);  
  55.         Button del = (Button) convertView.findViewById(R.id.id_remove);  
  56.         tv.setText(mDatas.get(position));  
  57.         del.setOnClickListener(new OnClickListener()  
  58.         {  
  59.             @Override  
  60.             public void onClick(View v)  
  61.             {  
  62.                 mDatas.remove(position);  
  63.                 notifyDataSetChanged();  
  64.                  /** 
  65.                   * 关闭SwipeListView 
  66.                   * 不关闭的话,刚删除位置的item存在问题 
  67.                   * 在监听事件中onListChange中关闭,会出现问题 
  68.                   */  
  69.                 mSwipeListView.closeOpenedItems();  
  70.             }  
  71.         });  
  72.           
  73.         return convertView;  
  74.     }  
  75.   
  76. }  

代码相当简单,MainActivity里面设置了监听事件,可以使用Demo的时候,观察触发的事件的输出,如果有特殊需求可以做一些操作。


源码点击下载

0 0
原创粉丝点击