前言:项目中经常会用到类似于QQ侧滑点击删除的效果,网上的开源库也很多。个人感觉SwipeLayout最好用。下面介绍怎么使用。
一、首先导入需要的Jar包,有3个,AndroidSwipeLayout-v1.1.8.jar、AndroidViewAnimations-1.1.3.jar、nineoldandroids-2.4.0.jar。第一个jar包就是我们使用该控件的jar包,后面2个是侧滑出现删除menu的动画所需的jar包。下面就是怎么使用了。
主布局的xml文件如下,就是一个ListView:
listview中的每个Item的布局如下:
上面的那个LinerLayout相当于bottomView,下面的那个相对应于SurfaceView。开始的时候SurfaceView显示在手机屏幕上,而bottomView则在屏幕外,随着手指的滑动渐渐显示在屏幕上。
Adapter.java如下:
package com.example.adapter; import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.daimajia.androidanimations.library.Techniques; import com.daimajia.androidanimations.library.YoYo; import com.daimajia.swipe.SimpleSwipeListener; import com.daimajia.swipe.SwipeLayout; import com.daimajia.swipe.adapters.BaseSwipeAdapter; import com.example.firstactivity.R; public class ListViewAdapter extends BaseSwipeAdapter { private Context mContext; private ListmDatas; //private TextView mDelete; //private SwipeLayout swipeLayout; private int pos ; public ListViewAdapter(Context context, List mDatas) { this.mContext = context; this.mDatas = mDatas; } @Override public int getCount() { // TODO Auto-generated method stub return mDatas.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mDatas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public void fillValues(int position, View convertView) { // TODO Auto-generated method stub Log.e("fillValues", "position = "+position); TextView tv = (TextView) convertView.findViewById(R.id.position); //tv.setText((position + 1) + "."); tv.setText(mDatas.get(position)+"...."); final SwipeLayout sl = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position)); final TextView delete = (TextView) convertView.findViewById(R.id.delete); delete.setTag(position); delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int pos = (Integer) delete.getTag(); String obj = mDatas.get(pos); Log.e("onClick", "........pos ...."+pos+" obj = "+obj); mDatas.remove(obj); notifyDataSetChanged(); sl.close(); } }); } @Override public View generateView(int position, ViewGroup arg1) { // TODO Auto-generated method stub Log.e("generateView", "position = "+position); View v = LayoutInflater.from(mContext).inflate(R.layout.swipe_lv_item,null); pos = position; final SwipeLayout swipeLayout = (SwipeLayout) v.findViewById(R.id.swipe); swipeLayout.addSwipeListener(new SimpleSwipeListener() { @Override public void onOpen(SwipeLayout layout) {//当隐藏的删除menu被打开的时候的回调函数 // TODO Auto-generated method stub YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash)); } }); swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() { @Override public void onDoubleClick(SwipeLayout layout, boolean surface) { Toast.makeText(mContext, "DoubleClick",Toast.LENGTH_SHORT).show(); } }); // v.findViewById(R.id.delete).setOnClickListener( // new View.OnClickListener() { // @Override // public void onClick(View view) { // Toast.makeText(mContext, "click delete position = "+pos,Toast.LENGTH_SHORT).show(); // swipeLayout.close(); // } // }); return v; } @Override public int getSwipeLayoutResourceId(int position) { // TODO Auto-generated method stub return R.id.swipe; } }
注意, generateView只是加布局,最好不要在里面设置点击事件。点击事件设置fillValues这个方法中。
Activity.java如下:
package com.example.firstactivity; import java.util.ArrayList; import java.util.List; import com.example.adapter.ListViewAdapter; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import com.daimajia.swipe.util.Attributes; public class SwipeListViewActivity extends Activity{ private ListView mListView; private ListViewAdapter mAdapter; private ListmDatas; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.swipe_layout_main); getData(); mListView = (ListView) findViewById(R.id.swipe_listview); mAdapter = new ListViewAdapter(this, mDatas); mAdapter.setMode(Attributes.Mode.Single); mListView.setAdapter(mAdapter); } public void getData(){ mDatas = new ArrayList (); //for(int i =0; i<10; i++){ mDatas.add("A"); mDatas.add("B"); mDatas.add("C"); mDatas.add("D"); mDatas.add("E"); mDatas.add("F"); mDatas.add("G"); mDatas.add("H"); mDatas.add("I"); mDatas.add("J"); //} } }