Android SwipeListView 实现ListView的滑动删除效果
jopen
10年前
今天在github上发现一个非常不错的开源项目 SwipeListView,官网地址: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); } }
细节不多说了,自己可以去看官网文档
XML Usage
If you decide to use SwipeListView as a view, you can define it in your xml layout like this:
<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" swipe:swipeBackView="@+id/back" swipe:swipeActionLeft="[reveal | dismiss]" swipe:swipeActionRight="[reveal | dismiss]" swipe:swipeMode="[none | both | right | left]" swipe:swipeCloseAllItemsWhenMoveList="[true | false]" swipe:swipeOpenOnLongPress="[true | false]" swipe:swipeAnimationTime="[miliseconds]" swipe:swipeOffsetLeft="[dimension]" swipe:swipeOffsetRight="[dimension]" />
swipeFrontView
- Required - front view id.swipeBackView
- Required - back view id.swipeActionLeft
- Optional - left swipe action Default: 'reveal'swipeActionRight
- Optional - right swipe action Default: 'reveal'swipeMode
- Gestures to enable or 'none'. Default: 'both'swipeCloseAllItemsWhenMoveList
- Close revealed items on list motion. Default: 'true'swipeOpenOnLongPress
- Reveal on long press Default: 'true'swipeAnimationTime
- item drop animation time. Default: android configurationswipeOffsetLeft
- left offsetswipeOffsetRight
- right offset