生活随笔
收集整理的這篇文章主要介紹了
android 仿微信侧滑删除SwipeListView实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原帖地址:http://www.eoeandroid.com/thread-540337-1-1.html
Android微信5.0以前某個版本(具體哪個忘記了)實現(xiàn)了和IOS上面一樣的效果——側(cè)滑刪除。這個特效在github上是一個比較火的開源項目,最近樓主也對一些UI特效比較感興趣,在收集這些炫酷又實用的實例,如果樓下那位朋友有其他比較好的實例,還請不吝賜教。
? ?? ? 下面是樓主仿微信側(cè)滑刪除做的實例部署后的截圖:
? ?? ????
?
? ?? ???點擊刪除之后可以刪除該行。
? ?? ????
?
? ?? ???下面是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]"//設(shè)置向左滑動是當(dāng)前別滑動item的顯示效果(reveal顯示背后的viewgroup,dismiss該item消失)? ? swipe:swipeActionRight="[reveal | dismiss]"//同向左滑動? ?? ?? ?? ?
? ?? ?? ?? ?swipe:swipeMode="[none | both | right | left]"//設(shè)置swipelistview無滑動效果,兩側(cè)均可滑動,向右滑動,向左滑動? ?? ?? ?? ?? ?? ?? ?? ? swipe:swipeCloseAllItemsWhenMoveList="[true | false]"//swipelistview滾動時關(guān)閉所有已打開的item? ?? ?? ?? ?swipe:swipeOpenOnLongPress="[true | false]"//長按某個item是否打開? ?? ?? ?? ?swipe:swipeAnimationTime="[miliseconds]"//動畫持續(xù)時間? ?? ?? ?? ?swipe:swipeOffsetLeft="[dimension]"//滑動后頂部viewgroup距離左邊界距離? ?? ?? ?? ?swipe:swipeOffsetRight="[dimension]"? ?? ?? ?? ?/> 復(fù)制代碼
? ?? ? 在上源碼之前,必須要向各位說明的是,使用第三方開源項目必需要引入相應(yīng)的jar包或者項目,SwipeListView必須依賴開源swipelistview項目以及nineoldandroids-2.4.0.jar,我在后面的附件會提供這些第三方工具。當(dāng)你在開發(fā)自己的swipelistview實例之前,需要把開源項目swipelistview先導(dǎo)入你的工程目錄,然后勾選iIs Library,之后新建你自己的項目,在項目中引入這個開源項目OK了。具體的如何引入這里不做贅述,相信接觸過開發(fā)的基本都懂。
下面是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("衛(wèi)斯理", "2013-5-20");
? ? ? ? ? ? ? ? TestData td6=new TestData("曹操", "2013-2-14");
? ? ? ? ? ? ? ? TestData td7=new TestData("劉備", "2012-8-15");
? ? ? ? ? ? ? ? TestData td8=new TestData("孫權(quán)", "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);
? ? ? ? }
? ? ? ??
}
復(fù)制代碼
下面是MainActiivty.java的布局文件main_layout.xml
<?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>
復(fù)制代碼
下面是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>
復(fù)制代碼
最后給出github中SwipeListView的連接
SwpieListView GitHub地址
項目源碼,我給出的源碼已經(jīng)包含第三方開源組件,希望自己寫出測試實例的可以直接使用
PS:源碼請到原帖http://www.eoeandroid.com/thread-540337-1-1.html下載
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的android 仿微信侧滑删除SwipeListView实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。