码迷,mamicode.com
首页 > 其他好文 > 详细

swipelistview使用方法

时间:2015-09-09 18:56:58      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

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();
}
}

 

swipelistview使用方法

标签:

原文地址:http://www.cnblogs.com/jkx1229761162/p/4795513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!