swipelistview 的xml类似这样:
<com.fortysevendeg.swipelistview.SwipeListView
android:id="@+id/swipe_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#ff0000"
app:swipeActionLeft="reveal"
app:swipeActionRight="reveal"
app:swipeAnimationTime="0"
app:swipeBackView="@+id/back" //这个id需要和item的back的id一致(front同理)
app:swipeCloseAllItemsWhenMoveList="true" //拖动listview时是否关闭拉开的item(有人建议是true,不然可能有问题)
app:swipeFrontView="@+id/front"
app:swipeMode="both" //是否左右都能拉
app:swipeOffsetLeft="0dp" //拉动距离
app:swipeOffsetRight="0dp"
app:swipeOpenOnLongPress="false" //长按打开item
/>
设置adapter:
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 ;
}
}
设置listener:
class TestSwipeListviewListener extends BaseSwipeListViewListener {
@Override
public void onClickFrontView(int position) {
// TODO Auto-generated method stub
super.onClickFrontView(position);
Toast.makeText(getApplicationContext(), "click:"+testdata.get(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onDismiss(int[] reverseSortedPositions) {
for(int position:reverseSortedPositions){
//这里弹出的toast必须在前面,不然显示的是下一个item
Toast.makeText(getApplicationContext(), "remove:"+testdata.get(position), Toast.LENGTH_SHORT).show();
testdata.remove(position);
}
adapter.notifyDataSetChanged();
}
}