QQ的滑动删除效果很不错,要实现这种效果,可以使用SwipeListVIEw。
1. 下载com.fortysevendeg.swipeListvIEw这个项目(以前GitHub上有,现在GitHub上没有了,百度了很多次才下载到的),导入Eclipse,右键单击,选择PropertIEs->AndroID,选中library下面的Islibrary。
2. 新建一个项目MySwipeListVIEw,加入SwipeListVIEw这个库。
3. 在主窗体里面放入一个SwipeListVIEw控件:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.hzhi.myswipeListvIEw.MainActivity" > <com.fortysevendeg.swipeListvIEw.SwipeListVIEw xmlns:swipe="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/exampleSwipeListVIEw" androID:ListSelector="#00000000" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" swipe:swipeBackVIEw="@+ID/back" swipe:swipeCloseAllitemsWhenMoveList="true" swipe:swipeDrawableChecked="@drawable/choice_selected" swipe:swipeDrawableunchecked="@drawable/choice_unselected" swipe:swipeFrontVIEw="@+ID/front" swipe:swipeMode="both" swipe:swipeActionleft="reveal" swipe:swipeActionRight="dismiss" swipe:swipeOpenOnLongPress="true" /></linearLayout>
其中两个重要的属性:
swipe:swipeFrontVIEw:上面的VIEw,即不滑动时显示的VIEw。
swipe:swipeBackVIEw:下面的VIEw,即滑动后显示的VIEw。
这两个VIEw都定义在SwipeListVIEw的行布局文件里面:
<?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 androID:ID="@+ID/back" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:background="#ffcccccc" androID:gravity="right" androID:tag="back" > <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/btn_delete" androID:text="删除"/> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/btn_update" androID:text="更新"/> </linearLayout> <relativeLayout androID:orIEntation="vertical" androID:ID="@+ID/front" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:background="#ffffffff" > <ImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/example_row_iv_image"/> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_toRightOf="@ID/example_row_iv_image" androID:ID="@+ID/example_row_tv_Title"/> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_toRightOf="@ID/example_row_iv_image" androID:layout_below="@ID/example_row_tv_Title" androID:ID="@+ID/example_row_tv_description"/> </relativeLayout> </FrameLayout>
SwipeListVIEw的行布局文件使用FrameLayout布局,FrameLayout里面所有的所有子元素都堆叠在FrameLayout的左上角。
4. SwipeListVIEw和其他ListVIEw一样,也需要Adapter,使用方法也是一样的。这里就不详细讲了。
5. 在主窗体Java文件中实现SwipeListVIEw的功能,代码如下:
package com.hzhi.myswipeListvIEw;import androID.support.v7.app.ActionBaractivity;import androID.util.Log;import java.util.ArrayList;import com.fortysevendeg.swipeListvIEw.BaseSwipeListVIEwListener;import com.fortysevendeg.swipeListvIEw.SwipeListVIEw;import androID.os.Bundle;@SuppressWarnings("deprecation")public class MainActivity extends ActionBaractivity { protected static final String TAG = "MySwipeListVIEw"; private ArrayList<String> mList; private MyAdapter mAdapter; private SwipeListVIEw mSwipeListVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initData(); mSwipeListVIEw = (SwipeListVIEw) findVIEwByID(R.ID.exampleSwipeListVIEw); mAdapter = new MyAdapter(this,mList,mSwipeListVIEw); mSwipeListVIEw.setAdapter(mAdapter); mSwipeListVIEw.setSwipeListVIEwListener(new BaseSwipeListVIEwListener(){ @OverrIDe public voID onChoiceChanged(int position,boolean selected) { Log.d(TAG,"onChoiceChanged:" + position + "," + selected); } @OverrIDe public voID onChoiceEnded() { Log.d(TAG,"onChoiceEnded"); } @OverrIDe public voID onChoiceStarted() { Log.d(TAG,"onChoiceStarted"); } @OverrIDe public voID onClickBackVIEw(int position) { Log.d(TAG,"onClickBackVIEw:" + position); } @OverrIDe public voID onClickFrontVIEw(int position) { Log.d(TAG,"onClickFrontVIEw:" + position); } @OverrIDe public voID onClosed(int position,boolean fromright) { Log.d(TAG,"onClosed:" + position + "," + fromright); } @OverrIDe public voID ondismiss(int[] reverseSortedpositions) { Log.d(TAG,"ondismiss"); } @OverrIDe public voID onFirstListItem() { Log.d(TAG,"onFirstListItem"); } @OverrIDe public voID onLastListItem() { Log.d(TAG,"onLastListItem"); } @OverrIDe public voID onListChanged() { Log.d(TAG,"onListChanged"); mSwipeListVIEw.cloSEOpenedItems(); } @OverrIDe public voID onMove(int position,float x) { Log.d(TAG,"onMove:" + position + "," + x); } @OverrIDe public voID onopened(int position,boolean toRight) { Log.d(TAG,"onopened:" + position + "," + toRight); } @OverrIDe public voID onStartClose(int position,boolean right) { Log.d(TAG,"onStartClose:" + position + "," + right); } @OverrIDe public voID onStartopen(int position,int action,"onStartopen:" + position + "," + action + "," + right); } }); } private voID initData(){ mList = new ArrayList<String>(); for (int i = 0; i <= 10; i++) mList.add("这是第" + i +"条数据!"); } }
最主要的代码即mSwipeListVIEw.setSwipeListVIEwListener(new BaseSwipeListVIEwListener(){}),通过这行代码,为SwipeListVIEw控件设置了Listener,可以根据自己的需要重载BaseSwipeListVIEwListener的各种方法。
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android使用SwipeListView实现类似QQ的滑动删除效果全部内容,希望文章能够帮你解决Android使用SwipeListView实现类似QQ的滑动删除效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)