SwipeListView的一些实例讲解

本文将为您介绍SwipeListView的使用方法,通过实例演示如何在Android应用中实现滑动删除功能,并提供源码及配置说明。
  • SwipeListView的一些实例讲解

  • 代码描述:

    SwipeListView的一些实例讲解

  • 托管地址:

  • 代码作者:

    caijia --- android_nihao




                    前几天有个同学在群里问微信的那个ListView的滑动删除功能,于是在网上找了下资料,发现GitHub上有个开源项目SwipeListView于是就试着做了一下,现在拿出来给大家分享.希望对大家有所帮助.

                    先准备一些前提工作;

                    一、下载swipelistview的开源项目、 https://github.com/47deg/android-swipelistview

                    二、调试 这里我给大家介绍一篇博客   http://blog.youkuaiyun.com/zhangxinrun/article/details/8965115 相信看来 之后大家就能调式出来了  不过后面我会把源码发布(已经调式好了)给大家。

                    三、准备上面两步之后就可以开始开发了。



                   把swipelistview库导进工程就可以看到swipelistview的jar在工程里了 下面来讲解一下我的这个Demo首先来看一下工程目录图, j

                   先看一下布局文件
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     xmlns:swipe="http://schemas.android.com/apk/res-auto"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context=".MainActivity" >

  7.     <com.fortysevendeg.swipelistview.SwipeListView
  8.         android:id="@+id/example_lv_list"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent"
  11.         android:listSelector="#00000000"
  12.         swipe:swipeActionLeft="reveal"
  13.         swipe:swipeActionRight="reveal"
  14.         swipe:swipeAnimationTime="0"
  15.         swipe:swipeBackView="@+id/back"
  16.         swipe:swipeCloseAllItemsWhenMoveList="true"
  17.         swipe:swipeFrontView="@+id/front"
  18.         swipe:swipeMode="both"
  19.         swipe:swipeOffsetLeft="0dp"
  20.         swipe:swipeOffsetRight="0dp"
  21.         swipe:swipeOpenOnLongPress="false" />

  22. </RelativeLayout>
复制代码

   上面这段代码主要是对swipelistview的一些配置。

    <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.
  • swipeBackView - Required - back view id.
  • swipeActionLeft - Optional - left swipe action Default: 'reveal'
  • swipeActionRight - Optional - right swipe action Default: 'reveal'
  • swipeMode - Gestures to enable or 'none'. Default: 'both'
  • swipeCloseAllItemsWhenMoveList - Close revealed items on list motion. Default: 'true'
  • swipeOpenOnLongPress - Reveal on long press Default: 'true'
  • swipeAnimationTime - item drop animation time. Default: android configuration
  • swipeOffsetLeft - left offset
  • swipeOffsetRight - right offset
          然后就是MainActivity里面的代码

         
  1. package cn.zhongyun.swipelistviewtest;

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
  6. import com.fortysevendeg.swipelistview.SwipeListView;
  7. import android.os.Bundle;
  8. import android.widget.Toast;
  9. import android.app.Activity;

  10. public class MainActivity extends Activity {
  11.         private SwipeListView mSwipeListView ;
  12.         private SwipeAdapter mAdapter ;
  13.         public static int deviceWidth ;
  14.         private List<String> testData ;
  15.         @Override
  16.         protected void onCreate(Bundle savedInstanceState) {
  17.                 super.onCreate(savedInstanceState);
  18.                 setContentView(R.layout.activity_main);
  19.                 mSwipeListView = (SwipeListView) findViewById(R.id.example_lv_list);
  20.                 testData = getTestData();
  21.                 mAdapter = new SwipeAdapter(this, R.layout.package_row, testData,mSwipeListView);
  22.                 deviceWidth = getDeviceWidth();
  23.                 mSwipeListView.setAdapter(mAdapter);
  24.                 mSwipeListView.setSwipeListViewListener( new TestBaseSwipeListViewListener());
  25.                 reload();
  26.         }

  27.         private List<String> getTestData() {
  28.                 String [] obj = new String[]{"背对背拥抱","第几个一百天","江南","那些你很冒险的梦","醉赤壁","西界","爱与希望","你要的不是我","不潮不用花钱","只对你有感觉","简简单单"};
  29.                 List<String> list = new ArrayList<String>(Arrays.asList(obj));
  30.                 return list;
  31.         }

  32.         private int getDeviceWidth() {
  33.                 return getResources().getDisplayMetrics().widthPixels;
  34.         }

  35.         private void reload() {
  36.                 mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);
  37.                 mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);
  38. //                mSwipeListView.setSwipeActionRight(settings.getSwipeActionRight());
  39.                 mSwipeListView.setOffsetLeft(deviceWidth * 1 / 3);
  40. //                mSwipeListView.setOffsetRight(convertDpToPixel(settings.getSwipeOffsetRight()));
  41.                 mSwipeListView.setAnimationTime(0);
  42.                 mSwipeListView.setSwipeOpenOnLongPress(false);
  43.     }
  44.         
  45.         class TestBaseSwipeListViewListener extends BaseSwipeListViewListener{

  46.                 @Override
  47.                 public void onClickFrontView(int position) {
  48.                         super.onClickFrontView(position);
  49.                         Toast.makeText(getApplicationContext(), testData.get(position), Toast.LENGTH_SHORT).show();
  50.                 }

  51.                 @Override
  52.                 public void onDismiss(int[] reverseSortedPositions) {
  53.                         for (int position : reverseSortedPositions) {
  54.                                 testData.remove(position);
  55.                         }
  56.                         mAdapter.notifyDataSetChanged();
  57.                 }
  58.         }
  59. }
复制代码
            SwipeAdapter里面的代码
  1. package cn.zhongyun.swipelistviewtest;

  2. import java.util.List;
  3. import com.fortysevendeg.swipelistview.SwipeListView;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.ViewGroup;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.TextView;

  12. public class SwipeAdapter extends ArrayAdapter<String> {
  13.         private LayoutInflater mInflater ;
  14.         private List<String> objects ;
  15.         private SwipeListView mSwipeListView ;
  16.         public SwipeAdapter(Context context, int textViewResourceId,List<String> objects, SwipeListView mSwipeListView) {
  17.                 super(context, textViewResourceId, objects);
  18.                 this.objects = objects ;
  19.                 this.mSwipeListView = mSwipeListView ;
  20.                 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  21.         }

  22.         @Override
  23.         public View getView(final int position, View convertView, ViewGroup parent) {
  24.                 ViewHolder holder = null ;
  25.                 if(convertView == null){
  26.                         convertView = mInflater.inflate(R.layout.package_row, parent, false);
  27.                         holder = new ViewHolder();
  28.                         holder.mFrontText = (TextView) convertView.findViewById(R.id.example_row_tv_title);
  29.                         holder.mBackEdit = (Button) convertView.findViewById(R.id.example_row_b_action_3);
  30.                         holder.mBackDelete = (Button) convertView.findViewById(R.id.example_row_b_action_2);
  31.                         convertView.setTag(holder);
  32.                 }else{
  33.                         holder = (ViewHolder) convertView.getTag();
  34.                 }
  35.                 holder.mBackDelete.setOnClickListener(new OnClickListener() {
  36.                         @Override
  37.                         public void onClick(View v) {
  38.                                 mSwipeListView.closeAnimate(position);
  39.                                 mSwipeListView.dismiss(position);
  40.                         }
  41.                 });
  42.                 String item = getItem(position);
  43.                 holder.mFrontText.setText(item);
  44.                 return convertView;
  45.         }
  46.         class ViewHolder{
  47.                 TextView mFrontText ;
  48.                 Button mBackEdit,mBackDelete ;
  49.         }
  50. }
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>


  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" >

  5.     <LinearLayout
  6.         android:id="@+id/back"
  7.         android:layout_width="match_parent"
  8.         android:layout_height="80dp"
  9.         android:tag="back"
  10.         android:background="#eee"
  11.          >

  12.         <Button
  13.             android:id="@+id/example_row_b_action_1"
  14.             android:layout_width="0dp"
  15.             android:background="@null"
  16.             android:layout_height="wrap_content"
  17.             android:layout_weight="1" />

  18.         <Button
  19.             android:id="@+id/example_row_b_action_2"
  20.             android:layout_width="0dp"
  21.             android:layout_marginLeft="10dp"
  22.             android:layout_height="60dp"
  23.             android:text="删除"
  24.             android:layout_gravity="center"
  25.             android:layout_weight="1" />

  26.         <Button
  27.             android:id="@+id/example_row_b_action_3"
  28.             android:layout_width="0dp"
  29.             android:text="编辑"
  30.             android:layout_gravity="center"
  31.             android:layout_height="60dp"
  32.             android:layout_weight="1" />
  33.     </LinearLayout>

  34.     <RelativeLayout
  35.         android:id="@+id/front"
  36.         android:orientation="vertical"
  37.         android:tag="front"
  38.         android:background="#ffffff"
  39.         android:layout_width="match_parent"
  40.         android:layout_height="80dp" >

  41.         <TextView
  42.             android:id="@+id/example_row_tv_title"
  43.             android:layout_width="fill_parent"
  44.             android:layout_height="wrap_content"
  45.             android:layout_centerInParent="true"
  46.             android:textSize="18sp"
  47.              />
  48.     </RelativeLayout>

  49. </FrameLayout>
先来看activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:swipe="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.fortysevendeg.swipelistview.SwipeListView android:id="@+id/example_lv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:listSelector="#00000000" swipe:swipeActionLeft="dismiss" swipe:swipeActionRight="reveal" swipe:swipeAnimationTime="0" swipe:swipeBackView="@+id/back" swipe:swipeCloseAllItemsWhenMoveList="true" swipe:swipeFrontView="@+id/front" swipe:swipeMode="both" swipe:swipeOffsetLeft="0dp" swipe:swipeOffsetRight="0dp" swipe:swipeOpenOnLongPress="false" /> </RelativeLayout> 这里就一个swipelistview控件,我说几个不易理解的属性 表示滑动时的操作,dismiss表示滑动时删除,如果设置为reveal表示滑动时会显示出item后面的选项 swipe:swipeActionLeft=”dismiss” swipe:swipeActionRight=”reveal” 这个是背面布局的id(我们把直接看到的布局叫做前面的,滑动之后才能看到的布局叫做背面的),必须背面布局id对应 swipe:swipeBackView=”@+id/back” 这个是滚动时候是否关闭背面的布局,true表示关闭,false表示不关闭,一般设置为true swipe:swipeCloseAllItemsWhenMoveList=”true” 这个是前面布局的id,要布局的id对应 swipe:swipeFrontView=”@+id/front” both表示可以向左滑也可以向右滑,right和left分别表示只能向有或者向左滑动。 swipe:swipeMode=”both” 下面两个表示向左或者向右滑动时的偏移量,一般不在xml文件中设置,而是在代码中根据设置的大小来设置偏移量。 swipe:swipeOffsetLeft=”0dp” swipe:swipeOffsetRight=”0dp” 再来看看Item布局文件,这里包括前面的和后面的,两个重叠在一起: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- linearlayout中的布局是每一项后面隐藏的布局 --> <LinearLayout android:id="@+id/back" android:layout_width="match_parent" android:layout_height="80dp" android:background="#eee" android:tag="back" > <Button android:id="@+id/example_row_b_action_1" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginRight="10dp" android:layout_weight="1" android:text="测试" /> <Button android:id="@+id/example_row_b_action_2" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_weight="1" android:text="删除" /> <Button android:id="@+id/example_row_b_action_3" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_weight="1" android:text="编辑" /> </LinearLayout> <!-- 这里是前台显示的布局 --> <RelativeLayout android:id="@+id/front" android:layout_width="match_parent" android:layout_height="80dp" android:background="#ffffff" android:orientation="vertical" android:tag="front" > <TextView android:id="@+id/example_row_tv_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="18sp" /> </RelativeLayout> </FrameLayout> 这个布局是一个常规布局,我就不解释了。 MainActivity.Java,关键地方都有注释 public class MainActivity extends Activity { private SwipeListView mSwipeListView ; private SwipeAdapter mAdapter ; public static int deviceWidth ; private List<String> testData ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSwipeListView = (SwipeListView) findViewById(R.id.example_lv_list); testData = getTestData(); //数据适配 mAdapter = new SwipeAdapter(this, R.layout.package_row, testData,mSwipeListView); //拿到设备宽度 deviceWidth = getDeviceWidth(); mSwipeListView.setAdapter(mAdapter); //设置事件监听 mSwipeListView.setSwipeListViewListener( new TestBaseSwipeListViewListener()); reload(); } private List<String> getTestData() { String [] obj = new String[]{"红楼梦","西游记","水浒传","管锥编","宋诗选注","三国演义","android开发高级编程","红楼梦","西游记","水浒传","管锥编","宋诗选注","三国演义","android开发高级编程"}; List<String> list = new ArrayList<String>(Arrays.asList(obj)); return list; } private int getDeviceWidth() { return getResources().getDisplayMetrics().widthPixels; } private void reload() { // mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT); // mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL); // mSwipeListView.setSwipeActionRight(settings.getSwipeActionRight()); //滑动时向左偏移量,根据设备的大小来决定偏移量的大小 mSwipeListView.setOffsetLeft(deviceWidth * 1 / 3); mSwipeListView.setOffsetRight(deviceWidth * 1 / 3); // mSwipeListView.setOffsetRight(convertDpToPixel(settings.getSwipeOffsetRight())); //设置动画时间 mSwipeListView.setAnimationTime(30); mSwipeListView.setSwipeOpenOnLongPress(false); } class TestBaseSwipeListViewListener extends BaseSwipeListViewListener{ //点击每一项的响应事件 @Override public void onClickFrontView(int position) { super.onClickFrontView(position); Toast.makeText(getApplicationContext(), testData.get(position), Toast.LENGTH_SHORT).show(); } //关闭事件 @Override public void onDismiss(int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { Log.i("lenve", "position--:"+position); testData.remove(position); } mAdapter.notifyDataSetChanged(); } } } 数据适配器: public class SwipeAdapter extends ArrayAdapter<String> { private LayoutInflater mInflater ; private List<String> objects ; private SwipeListView mSwipeListView ; public SwipeAdapter(Context context, int textViewResourceId,List<String> objects, SwipeListView mSwipeListView) { super(context, textViewResourceId, objects); this.objects = objects ; this.mSwipeListView = mSwipeListView ; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null ; if(convertView == null){ convertView = mInflater.inflate(R.layout.package_row, parent, false); holder = new ViewHolder(); holder.mFrontText = (TextView) convertView.findViewById(R.id.example_row_tv_title); holder.mBackEdit = (Button) convertView.findViewById(R.id.example_row_b_action_3); holder.mBackDelete = (Button) convertView.findViewById(R.id.example_row_b_action_2); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.mBackDelete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //关闭动画 mSwipeListView.closeAnimate(position); //调用dismiss方法删除该项(这个方法在MainActivity中) mSwipeListView.dismiss(position); } }); String item = getItem(position); holder.mFrontText.setText(item); return convertView; } class ViewHolder{ TextView mFrontText ; Button mBackEdit,mBackDelete ; } } 以上就是SwipeListViewTest的用法,希望对你有帮助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值