Android 实现ListView的滑动删除效果--SwipeListView

官网地址:https://github.com/47deg/android-swipelistview

它可以实现ListView的滑动相关的一些效果,例如类似微信 ListView滑动删除效果等等,自己写了一个Demo Mark 一下。

 

 

activity_main.xml

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:swipe="http://schemas.android.com/apk/res-auto"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/background_app"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <com.fortysevendeg.swipelistview.SwipeListView  
  9.         android:id="@+id/mSwipeListView"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:listSelector="#00000000"  
  13.         swipe:swipeBackView="@+id/back"  
  14.         swipe:swipeCloseAllItemsWhenMoveList="true"  
  15.         swipe:swipeDrawableChecked="@drawable/choice_selected"  
  16.         swipe:swipeDrawableUnchecked="@drawable/choice_unselected"  
  17.         swipe:swipeFrontView="@+id/front"  
  18.         swipe:swipeMode="both" />  
  19.   
  20. </LinearLayout>  



 

BookAdapter.java

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.exampleswipelistviewtest.adapter;  
  2.   
  3. import java.util.List;  
  4. import android.content.Context;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13. import com.exampleswipelistviewtest.R;  
  14. import com.exampleswipelistviewtest.entity.Book;  
  15. import com.fortysevendeg.swipelistview.SwipeListView;  
  16.   
  17. public class BookAdapter extends BaseAdapter {  
  18.   
  19.     private List<Book> data;  
  20.     private Context context;  
  21.     private LayoutInflater minInflater;  
  22.   
  23.     public BookAdapter(Context context, List<Book> data) {  
  24.         this.context = context;  
  25.         this.data = data;  
  26.           
  27.         minInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  28.     }  
  29.   
  30.     @Override  
  31.     public int getCount() {  
  32.         return data.size();  
  33.     }  
  34.   
  35.     @Override  
  36.     public Book getItem(int position) {  
  37.         return data.get(position);  
  38.     }  
  39.   
  40.     @Override  
  41.     public long getItemId(int position) {  
  42.         return position;  
  43.     }  
  44.   
  45.     @Override  
  46.     public View getView(final int position, View convertView, ViewGroup parent) {  
  47.         final Book item = getItem(position);  
  48.         ViewHolder holder;  
  49.         if (convertView == null) {  
  50.               
  51.             convertView = minInflater.inflate(R.layout.package_row, parent, false);  
  52.               
  53.             holder = new ViewHolder();  
  54.             holder.ivLogo = (ImageView) convertView.findViewById(R.id.iv_logo);  
  55.             holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);  
  56.             holder.tvDescription = (TextView) convertView.findViewById(R.id.tv_description);  
  57.             holder.btDelete = (Button) convertView.findViewById(R.id.bt_delete);  
  58.             holder.btEdit = (Button) convertView.findViewById(R.id.bt_edit);  
  59.               
  60.             convertView.setTag(holder);  
  61.         } else {  
  62.             holder = (ViewHolder) convertView.getTag();  
  63.         }  
  64.   
  65.         ((SwipeListView)parent).recycle(convertView, position);  
  66.   
  67.         holder.ivLogo.setImageResource(item.getLogo());  
  68.         holder.tvTitle.setText(item.getName());  
  69.         holder.tvDescription.setText(item.getDesc());  
  70.   
  71.         holder.btDelete.setOnClickListener(new View.OnClickListener() {  
  72.             @Override  
  73.             public void onClick(View v) {  
  74.                  
  75.                 Toast.makeText(context, R.string.delete, Toast.LENGTH_SHORT).show();  
  76.             }  
  77.         });  
  78.   
  79.         holder.btEdit.setOnClickListener(new View.OnClickListener() {  
  80.             @Override  
  81.             public void onClick(View v) {  
  82.                   
  83.                 Toast.makeText(context, R.string.edit, Toast.LENGTH_SHORT).show();  
  84.             }  
  85.         });  
  86.   
  87.         return convertView;  
  88.     }  
  89.   
  90.     static class ViewHolder {  
  91.         ImageView ivLogo;  
  92.         TextView tvTitle;  
  93.         TextView tvDescription;  
  94.         Button btEdit;  
  95.         Button btDelete;  
  96.     }  
  97.   
  98. }  



 

MainActivity.java

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
    1. package com.exampleswipelistviewtest;  
    2.   
    3. import java.util.ArrayList;  
    4. import android.app.Activity;  
    5. import android.os.Bundle;  
    6. import android.util.Log;  
    7. import com.exampleswipelistviewtest.adapter.BookAdapter;  
    8. import com.exampleswipelistviewtest.entity.Book;  
    9. import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;  
    10. import com.fortysevendeg.swipelistview.SwipeListView;  
    11.   
    12. public class MainActivity extends Activity {  
    13.   
    14.     private static final String TAG = MainActivity.class.getSimpleName();  
    15.     private ArrayList<Book> data = new ArrayList<Book>();  
    16.     private BookAdapter mAdapter;  
    17.     private SwipeListView mSwipeListView;  
    18.   
    19.     @Override  
    20.     protected void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.         setContentView(R.layout.activity_main);  
    23.   
    24.         initData();  
    25.         findView();  
    26.     }  
    27.   
    28.     private void initData() {  
    29.           
    30.         for(int i=0;i<20;i++){  
    31.               
    32.             Book book = new Book();  
    33.             book.setLogo(R.drawable.ic_launcher);  
    34.             book.setName("研磨设计模式 "+i);  
    35.             book.setDesc("一本好书 "+i);  
    36.               
    37.             data.add(book);  
    38.         }  
    39.     }  
    40.   
    41.     private void findView() {  
    42.   
    43.         mAdapter = new BookAdapter(this, data);  
    44.   
    45.         mSwipeListView = (SwipeListView) findViewById(R.id.mSwipeListView);  
    46.           
    47.         mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {  
    48.   
    49.             @Override  
    50.             public void onStartOpen(int position, int action,  
    51.                     boolean right) {  
    52.   
    53.                 Log.d(TAG, "onStartOpen");  
    54.             }  
    55.   
    56.             @Override  
    57.             public void onStartClose(int position, boolean right) {  
    58.   
    59.                 Log.d(TAG, "onStartClose");  
    60.             }  
    61.   
    62.             @Override  
    63.             public void onClickFrontView(int position) {  
    64.   
    65.                 Log.d(TAG, "onClickFrontView");  
    66.             }  
    67.   
    68.             @Override  
    69.             public void onClickBackView(int position) {  
    70.   
    71.                 Log.d(TAG, "onClickBackView");  
    72.             }  
    73.   
    74.             @Override  
    75.             public void onDismiss(int[] reverseSortedPositions) {  
    76.   
    77.                 Log.d(TAG, "onDismiss");  
    78.                 for (int position : reverseSortedPositions) {  
    79.                     data.remove(position);  
    80.                 }  
    81.                 mAdapter.notifyDataSetChanged();  
    82.             }  
    83.         });  
    84.           
    85.         mSwipeListView.setAdapter(mAdapter);  
    86.           
    87.         mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH);  
    88.         mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);  
    89.         mSwipeListView.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);  
    90.         mSwipeListView.setOffsetLeft(getResources().getDimension(R.dimen.left_offset));  
    91.         mSwipeListView.setOffsetRight(getResources().getDimension(R.dimen.right_offset));  
    92.         mSwipeListView.setAnimationTime(200);  
    93.         mSwipeListView.setSwipeOpenOnLongPress(true);  
    94.     }  
    95.   
    96. }  

转载于:https://www.cnblogs.com/weixiao870428/p/3524052.html

先来看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、付费专栏及课程。

余额充值