您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何正確的使用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"); //} } }
關于如何正確的使用SwipeLayout側滑刪除控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。