仿微信侧滑删除SwipeListView实例

来源:互联网 发布:大数据质量管理 编辑:程序博客网 时间:2024/06/05 11:02

必须要向各位说明的是,使用第三方开源项目必需要引入相应的jar包或者项目,SwipeListView必须依赖开源swipelistview项目以及nineoldandroids-2.4.0.jar,

源码下载:https://github.com/47deg/android-swipelistview。

 1、导入swipeListView库,报错需要nineoldandroids.jar和android-support-v4.jar库。android-support-v4.jar直接在Android SDK里的.\extras\android\support文件夹里找到。nineoldandroids.jar库需要到https://github.com/JakeWharton/NineOldAndroids/里下载。


  1. <com.fortysevendeg.swipelistview.SwipeListView
  2.             xmlns:swipe="http://schemas.android.com/apk/res-auto"//命名空间            
  3.              android:id="@+id/example_lv_list           
  4.              android:listSelector="#00000000"            
  5.              android:layout_width="fill_parent"            
  6.              android:layout_height="wrap_content"            
  7.              swipe:swipeFrontView="@+id/front"//swipelistview顶部viewgroup(实例截图中包含人物、头像、时间的layout)                                 swipe:swipeBackView="@+id/back"//swipelistview背后的viewgroup(实例截图中包删除按钮的layout)
  8.              swipe:swipeActionLeft="[reveal | dismiss]"//设置向左滑动是当前别滑动item的显示效果(reveal显示背后的viewgroup,dismiss该item消失)   
  9.                          swipe:swipeActionRight="[reveal | dismiss]"//同向左滑动            
  10.              swipe:swipeMode="[none | both | right | left]"//设置swipelistview无滑动效果,两侧均可滑动,向右滑动,向左滑动                           swipe:swipeCloseAllItemsWhenMoveList="[true | false]"//swipelistview滚动时关闭所有已打开的item        
  11.              swipe:swipeOpenOnLongPress="[true | false]"//长按某个item是否打开 
  12.              swipe:swipeAnimationTime="[miliseconds]"//动画持续时间       
  13.              swipe:swipeOffsetLeft="[dimension]"//滑动后顶部viewgroup距离左边界距离    
  14.              swipe:swipeOffsetRight="[dimension]"            />




                   把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>

转载自:http://www.apkbus.com/android-143803-1-1.html

0 0