原帖地址:http://www.eoeandroid.com/thread-540337-1-1.html
Android微信5.0以前某个版本(具体哪个忘记了)实现了和IOS上面一样的效果——侧滑删除。这个特效在github上是一个比较火的开源项目,最近楼主也对一些UI特效比较感兴趣,在收集这些炫酷又实用的实例,如果楼下那位朋友有其他比较好的实例,还请不吝赐教。
下面是楼主仿微信侧滑删除做的实例部署后的截图:![](http://a1.eoeandroid.com/attachment/forum/201408/14/111947heiss88e86evaeii.jpg)
点击删除之后可以删除该行。
![](http://a1.eoeandroid.com/attachment/forum/201408/14/112003tnrnfr54z53nl8nx.jpg)
下面是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"//swipelistview顶部viewgroup(实例截图中包含人物、头像、时间的layout) swipe:swipeBackView="@+id/back"//swipelistview背后的viewgroup(实例截图中包删除按钮的layout)
- swipe:swipeActionLeft="[reveal | dismiss]"//设置向左滑动是当前别滑动item的显示效果(reveal显示背后的viewgroup,dismiss该item消失) swipe:swipeActionRight="[reveal | dismiss]"//同向左滑动
- swipe:swipeMode="[none | both | right | left]"//设置swipelistview无滑动效果,两侧均可滑动,向右滑动,向左滑动 swipe:swipeCloseAllItemsWhenMoveList="[true | false]"//swipelistview滚动时关闭所有已打开的item swipe:swipeOpenOnLongPress="[true | false]"//长按某个item是否打开 swipe:swipeAnimationTime="[miliseconds]"//动画持续时间 swipe:swipeOffsetLeft="[dimension]"//滑动后顶部viewgroup距离左边界距离 swipe:swipeOffsetRight="[dimension]" />
在上源码之前,必须要向各位说明的是,使用第三方开源项目必需要引入相应的jar包或者项目,SwipeListView必须依赖开源swipelistview项目以及nineoldandroids-2.4.0.jar,我在后面的附件会提供这些第三方工具。当你在开发自己的swipelistview实例之前,需要把开源项目swipelistview先导入你的工程目录,然后勾选iIs Library,之后新建你自己的项目,在项目中引入这个开源项目OK了。具体的如何引入这里不做赘述,相信接触过开发的基本都懂。 下面是MainActivity.java
- package com.swipe.activity;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Toast;
-
- import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
- import com.fortysevendeg.swipelistview.SwipeListView;
-
- public class MainActivity extends Activity {
-
- private SwipeListView mSwipeListView;
- private SwipeListViewAdapter adapter;
- private List<TestData> datas;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_layout);
- mSwipeListView=(SwipeListView) findViewById(R.id.example_lv_list);
- mSwipeListView.setOffsetLeft(this.getResources().getDisplayMetrics().widthPixels*2/3);
- // mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);
- // mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);
- // mSwipeListView.setAnimationTime(0);
- // mSwipeListView.setSwipeOpenOnLongPress(false);
- initDatas();
- adapter=new SwipeListViewAdapter(this, mSwipeListView, datas);
- mSwipeListView.setAdapter(adapter);
- mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener(){
-
- @Override
- public void onClickFrontView(int position) {
- super.onClickFrontView(position);
- mSwipeListView.closeOpenedItems();
- Toast.makeText(MainActivity.this, adapter.getItem(position).getTime()+getTitle()+"和你对话", 2400).show();
- }
-
- @Override
- public void onDismiss(int[] reverseSortedPositions) {
- super.onDismiss(reverseSortedPositions);
- for (int i : reverseSortedPositions) {
- datas.remove(i);
- }
- adapter.notifyDataSetChanged();
- }
-
- });
- }
-
- private void initDatas(){
- datas=new ArrayList<TestData>();
- TestData td1=new TestData("张三", "2014-7-11");
- datas.add(td1);
- TestData td2=new TestData("李四", "2014-3-18");
- datas.add(td2);
- TestData td3=new TestData("王五", "2013-11-11");
- TestData td4=new TestData("白素", "2013-7-11");
- TestData td5=new TestData("卫斯理", "2013-5-20");
- TestData td6=new TestData("曹操", "2013-2-14");
- TestData td7=new TestData("刘备", "2012-8-15");
- TestData td8=new TestData("孙权", "2012-7-7");
- TestData td9=new TestData("董卓", "2012-2-14");
- TestData td10=new TestData("王允", "2011-10-1");
- TestData td11=new TestData("貂蝉", "2011-7-7");
- datas.add(td3);
- datas.add(td4);
- datas.add(td5);
- datas.add(td6);
- datas.add(td7);
- datas.add(td8);
- datas.add(td9);
- datas.add(td10);
- datas.add(td11);
- }
-
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <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="fill_parent"
- swipe:swipeFrontView="@+id/front"
- swipe:swipeBackView="@+id/back"
- swipe:swipeActionLeft="reveal"
- swipe:swipeActionRight="reveal"
- swipe:swipeMode="left"
- swipe:swipeCloseAllItemsWhenMoveList="true"
- swipe:swipeOpenOnLongPress="false"
- swipe:swipeAnimationTime="1"
- swipe:swipeOffsetLeft="0dip"
- swipe:swipeOffsetRight="0dp"
- />
-
- </LinearLayout>
下面是swipelistview自定义item package_row.xml
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <RelativeLayout
- android:id="@+id/back"
- android:tag="back"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="#eee"
- android:gravity="right">
- <Button
- android:id="@+id/delete"
- android:layout_width="wrap_content"
- android:layout_height="50dp"
- android:background="#FF0000"
- android:layout_alignParentRight="true"
- android:text="刪除"/>
- </RelativeLayout>
-
- <RelativeLayout
- android:id="@+id/front"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center_vertical"
- android:background="#ffffff"
- android:tag="front" >
-
- <ImageView
- android:id="@+id/head_icon"
- android:layout_width="45dp"
- android:layout_height="45dp"
- android:layout_marginLeft="20dp"
- android:background="@drawable/ic_launcher"
- android:scaleType="fitXY" />
- <TextView
- android:id="@+id/nickname"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textStyle="bold"
- android:textColor="@android:color/black"
- android:layout_toRightOf="@id/head_icon"
- android:layout_alignTop="@id/head_icon"
- android:textSize="18sp"/>
- <TextView android:id="@+id/time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentBottom="true"
- android:layout_marginRight="20dp"
- android:layout_marginBottom="5dp"
- />
- </RelativeLayout>
-
- </FrameLayout>
最后给出github中SwipeListView的连接 SwpieListView GitHub地址
项目源码,我给出的源码已经包含第三方开源组件,希望自己写出测试实例的可以直接使用