主activity 配置
package cn.zhongyun.swipelistviewtest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.SwipeListView;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
private SwipeListView mSwipeListView ;
private SwipeAdapter mAdapter ;
public static int deviceWidth ;
private List<String> testData ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeListView = (SwipeListView) findViewById(R.id.example_lv_list);
testData = getTestData();
mAdapter = new SwipeAdapter(this, R.layout.package_row, testData,mSwipeListView);
deviceWidth = getDeviceWidth();
mSwipeListView.setAdapter(mAdapter);
mSwipeListView.setSwipeListViewListener( new TestBaseSwipeListViewListener());
reload();
}
private List<String> getTestData() {
String [] obj = new String[]{"背对背拥抱","第几个一百天","江南","那些你很冒险的梦","醉赤壁","西界","爱与希望","你要的不是我","不潮不用花钱","只对你有感觉","简简单单"};
List<String> list = new ArrayList<String>(Arrays.asList(obj));
return list;
}
private int getDeviceWidth() {
return getResources().getDisplayMetrics().widthPixels;
}
private void reload() {
// 设置模式支持滑动
mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);
mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);
//mSwipeListView.setSwipeActionRight((SwipeListView.SWIPE_ACTION_REVEAL);
mSwipeListView.setOffsetLeft(deviceWidth * 1/ 3);
// mSwipeListView.setOffsetRight(convertDpToPixel(settings.getSwipeOffsetRight()));
mSwipeListView.setAnimationTime(0);
// 支持长按自动呼出
mSwipeListView.setSwipeOpenOnLongPress(true);
}
class TestBaseSwipeListViewListener extends BaseSwipeListViewListener{
@Override
public void onClickFrontView(int position) {
super.onClickFrontView(position);
Toast.makeText(getApplicationContext(), testData.get(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onDismiss(int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
testData.remove(position);
}
mAdapter.notifyDataSetChanged();
}
}
}
adapter :
package cn.zhongyun.swipelistviewtest;
import java.util.List;
import com.fortysevendeg.swipelistview.SwipeListView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class SwipeAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater ;
private List<String> objects ;
private SwipeListView mSwipeListView ;
public SwipeAdapter(Context context, int textViewResourceId,List<String> objects, SwipeListView mSwipeListView) {
super(context, textViewResourceId, objects);
this.objects = objects ;
this.mSwipeListView = mSwipeListView ;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null ;
if(convertView == null){
convertView = mInflater.inflate(R.layout.package_row, parent, false);
holder = new ViewHolder();
holder.mFrontText = (TextView) convertView.findViewById(R.id.example_row_tv_title);
holder.mBackEdit = (Button) convertView.findViewById(R.id.example_row_b_action_3);
holder.mBackDelete = (Button) convertView.findViewById(R.id.example_row_b_action_2);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.mBackDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSwipeListView.closeAnimate(position);
mSwipeListView.dismiss(position);
}
});
String item = getItem(position);
holder.mFrontText.setText(item);
return convertView;
}
class ViewHolder{
TextView mFrontText ;
Button mBackEdit,mBackDelete ;
}
}
activity_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.fortysevendeg.swipelistview.SwipeListView
android:id="@+id/example_lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#00000000"
swipe:swipeActionLeft="reveal"
swipe:swipeActionRight="reveal"
swipe:swipeAnimationTime="0"
swipe:swipeBackView="@+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="@+id/front"
swipe:swipeMode="both"
swipe:swipeOffsetLeft="0dp"
swipe:swipeOffsetRight="0dp"
swipe:swipeOpenOnLongPress="false" />
</RelativeLayout>
item layout
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2013 47 Degrees, LLC
~ http://47deg.com
~ hello@47deg.com
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<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="match_parent"
android:layout_height="80dp"
android:tag="back"
android:background="#eee"
>
<Button
android:id="@+id/example_row_b_action_1"
android:layout_width="0dp"
android:background="@null"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/example_row_b_action_2"
android:layout_width="0dp"
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:text="删除"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="@+id/example_row_b_action_3"
android:layout_width="0dp"
android:text="编辑"
android:layout_gravity="center"
android:layout_height="60dp"
android:layout_weight="1" />
</LinearLayout>
<RelativeLayout
android:id="@+id/front"
android:orientation="vertical"
android:tag="front"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="80dp" >
<TextView
android:id="@+id/example_row_tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="18sp"
/>
</RelativeLayout>
</FrameLayout>
库依赖nine 包,原理暂时不分析,有机会再完善本博。