官网地址:https://github.com/47deg/android-swipelistview。
它可以实现ListView的滑动相关的一些效果,例如类似微信 ListView滑动删除效果等等,自己写了一个Demo Mark 一下。
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:swipe="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/background_app"
- android:orientation="vertical" >
- <com.fortysevendeg.swipelistview.SwipeListView
- android:id="@+id/mSwipeListView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:listSelector="#00000000"
- swipe:swipeBackView="@+id/back"
- swipe:swipeCloseAllItemsWhenMoveList="true"
- swipe:swipeDrawableChecked="@drawable/choice_selected"
- swipe:swipeDrawableUnchecked="@drawable/choice_unselected"
- swipe:swipeFrontView="@+id/front"
- swipe:swipeMode="both" />
- </LinearLayout>
BookAdapter.java
- package com.exampleswipelistviewtest.adapter;
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.exampleswipelistviewtest.R;
- import com.exampleswipelistviewtest.entity.Book;
- import com.fortysevendeg.swipelistview.SwipeListView;
- public class BookAdapter extends BaseAdapter {
- private List<Book> data;
- private Context context;
- private LayoutInflater minInflater;
- public BookAdapter(Context context, List<Book> data) {
- this.context = context;
- this.data = data;
- minInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- @Override
- public int getCount() {
- return data.size();
- }
- @Override
- public Book getItem(int position) {
- return data.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- final Book item = getItem(position);
- ViewHolder holder;
- if (convertView == null) {
- convertView = minInflater.inflate(R.layout.package_row, parent, false);
- holder = new ViewHolder();
- holder.ivLogo = (ImageView) convertView.findViewById(R.id.iv_logo);
- holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
- holder.tvDescription = (TextView) convertView.findViewById(R.id.tv_description);
- holder.btDelete = (Button) convertView.findViewById(R.id.bt_delete);
- holder.btEdit = (Button) convertView.findViewById(R.id.bt_edit);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- ((SwipeListView)parent).recycle(convertView, position);
- holder.ivLogo.setImageResource(item.getLogo());
- holder.tvTitle.setText(item.getName());
- holder.tvDescription.setText(item.getDesc());
- holder.btDelete.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(context, R.string.delete, Toast.LENGTH_SHORT).show();
- }
- });
- holder.btEdit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(context, R.string.edit, Toast.LENGTH_SHORT).show();
- }
- });
- return convertView;
- }
- static class ViewHolder {
- ImageView ivLogo;
- TextView tvTitle;
- TextView tvDescription;
- Button btEdit;
- Button btDelete;
- }
- }
MainActivity.java
- package com.exampleswipelistviewtest;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import com.exampleswipelistviewtest.adapter.BookAdapter;
- import com.exampleswipelistviewtest.entity.Book;
- import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
- import com.fortysevendeg.swipelistview.SwipeListView;
- public class MainActivity extends Activity {
- private static final String TAG = MainActivity.class.getSimpleName();
- private ArrayList<Book> data = new ArrayList<Book>();
- private BookAdapter mAdapter;
- private SwipeListView mSwipeListView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initData();
- findView();
- }
- private void initData() {
- for(int i=0;i<20;i++){
- Book book = new Book();
- book.setLogo(R.drawable.ic_launcher);
- book.setName("研磨设计模式 "+i);
- book.setDesc("一本好书 "+i);
- data.add(book);
- }
- }
- private void findView() {
- mAdapter = new BookAdapter(this, data);
- mSwipeListView = (SwipeListView) findViewById(R.id.mSwipeListView);
- mSwipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
- @Override
- public void onStartOpen(int position, int action,
- boolean right) {
- Log.d(TAG, "onStartOpen");
- }
- @Override
- public void onStartClose(int position, boolean right) {
- Log.d(TAG, "onStartClose");
- }
- @Override
- public void onClickFrontView(int position) {
- Log.d(TAG, "onClickFrontView");
- }
- @Override
- public void onClickBackView(int position) {
- Log.d(TAG, "onClickBackView");
- }
- @Override
- public void onDismiss(int[] reverseSortedPositions) {
- Log.d(TAG, "onDismiss");
- for (int position : reverseSortedPositions) {
- data.remove(position);
- }
- mAdapter.notifyDataSetChanged();
- }
- });
- mSwipeListView.setAdapter(mAdapter);
- mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH);
- mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);
- mSwipeListView.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
- mSwipeListView.setOffsetLeft(getResources().getDimension(R.dimen.left_offset));
- mSwipeListView.setOffsetRight(getResources().getDimension(R.dimen.right_offset));
- mSwipeListView.setAnimationTime(200);
- mSwipeListView.setSwipeOpenOnLongPress(true);
- }
- }