非常实用的侧滑删除控件SwipeLayout
前言:项目中经常会用到类似于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:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/swipe_listview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
listview中的每个Item的布局如下:
<?xml version="1.0" encoding="utf-8"?> <com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:background="#FF5534" android:gravity="center" android:tag="Bottom3" android:weightSum="10" > <ImageView android:id="@+id/trash" android:layout_width="27dp" android:layout_height="30dp" android:layout_weight="1" android:src="@drawable/trash" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:text="Delete Item?" android:textColor="#fff" android:textSize="17sp" /> <Button android:id="@+id/delete" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="4" android:background="@drawable/white" android:text="Yes,Delete" android:textColor="#FF5534" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/item_selector" android:padding="10dp" > <TextView android:id="@+id/position" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_data" android:layout_width="match_parent" android:layout_height="match_parent" android:tag="Hover" android:text="Just Do it . " /> </LinearLayout> </com.daimajia.swipe.SwipeLayout>
上面的那个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 List<String> mDatas; //private TextView mDelete; //private SwipeLayout swipeLayout; private int pos ; public ListViewAdapter(Context context, List<String> 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 List<String> mDatas; @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<String>(); //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"); //} } }
上一篇:快速解决fragment中onActivityResult不调用的问题
栏 目:Android代码
下一篇:android studio 新建项目报错的解决之路
本文地址:http://www.codeinn.net/misctech/63686.html