SwipeListView 详解 实现微信,QQ等滑动删除效果

转载请标明出处:

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

效果图:

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

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

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、布局文件

<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:clickable="true"android:orientation="vertical" xmlns:swipe=""><includelayout="@layout/main_title"android:focusable="true" /><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent" ><com.fortysevendeg.swipelistview.SwipeListViewandroid:id="@+id/id_swipelistview"android:layout_width="fill_parent"android:layout_height="fill_parent"swipe:swipeActionLeft="reveal"swipe:swipeBackView="@+id/id_back"swipe:swipeCloseAllItemsWhenMoveList="true"swipe:swipeFrontView="@+id/id_front"swipe:swipeMode="left"swipe:swipeOffsetLeft="200dip"swipe:swipeOpenOnLongPress="false" /><TextViewandroid:id="@+id/empty"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:drawableTop="@drawable/no_chat"android:text="木有联系人"android:textColor="#ffb7b7b7"android:textSize="14.0sp"android:visibility="gone" /></FrameLayout><requestFocus /></LinearLayout>item的布局文件:

<FrameLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="60dp" ><LinearLayoutandroid:id="@+id/id_back"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffcccccc"android:gravity="center|right" ><Buttonandroid:id="@+id/id_remove"android:layout_width="60dp"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginRight="4dp"android:background="@drawable/red_button"android:text="Delete"android:textColor="#fff" ></Button></LinearLayout><LinearLayoutandroid:id="@+id/id_front"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffffff" ><TextViewandroid:id="@+id/id_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:gravity="center_vertical"android:minHeight="?android:attr/listPreferredItemHeight"android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="#000"android:textSize="25sp" ></TextView></LinearLayout></FrameLayout>注意对应布局的id和swipeListView中的frontView和backView的Id一致。

2、MainActivity

package com.example.zhy_swipelistview02;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Window;import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;import com.fortysevendeg.swipelistview.SwipeListView;public class MainActivity extends Activity{protected static final String TAG = "Activity";private SwipeListView mSwipeListView;private DataAdapter mAdapter;private List<String> mDatas;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initDatas();mSwipeListView = (SwipeListView) findViewById(R.id.id_swipelistview);mAdapter = new DataAdapter(this, mDatas , mSwipeListView);mSwipeListView.setAdapter(mAdapter);mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener(){@Overridepublic void onChoiceChanged(int position, boolean selected){Log.d(TAG, "onChoiceChanged:" + position + ", " + selected);}@Overridepublic void onChoiceEnded(){Log.d(TAG, "onChoiceEnded");}@Overridepublic void onChoiceStarted(){Log.d(TAG, "onChoiceStarted");}@Overridepublic void onClickBackView(int position){Log.d(TAG, "onClickBackView:" + position);}@Overridepublic void onClickFrontView(int position){Log.d(TAG, "onClickFrontView:" + position);}@Overridepublic void onClosed(int position, boolean fromRight){Log.d(TAG, "onClosed:" + position + "," + fromRight);}@Overridepublic void onDismiss(int[] reverseSortedPositions){Log.d(TAG, "onDismiss");}@Overridepublic void onFirstListItem(){Log.d(TAG, "onFirstListItem");}@Overridepublic void onLastListItem(){Log.d(TAG, "onLastListItem");}@Overridepublic void onListChanged(){Log.d(TAG, "onListChanged");mSwipeListView.closeOpenedItems();}@Overridepublic void onMove(int position, float x){Log.d(TAG, "onMove:" + position + "," + x);}@Overridepublic void onOpened(int position, boolean toRight){Log.d(TAG, "onOpened:" + position + "," + toRight);}@Overridepublic void onStartClose(int position, boolean right){Log.d(TAG, "onStartClose:" + position + "," + right);}@Overridepublic void onStartOpen(int position, int action, boolean right){Log.d(TAG, "onStartOpen:" + position + "," + action + ","+ right);}});}private void initDatas(){mDatas = new ArrayList<String>();for (int i = 'A'; i <= 'Z'; i++)mDatas.add((char) i + "");}}Adapter:

学习不是人生的全部,但学习都征服不了,那我还能做什么?

SwipeListView 详解 实现微信,QQ等滑动删除效果

相关文章:

你感兴趣的文章:

标签云: