First look at Activity_main.xml.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "xmlns:swipe=" Http://schemas.android.com/apk/res-auto "android:layout_width=" Match_ Parent "android:layout_height=" Match_parent "tools:context=". Mainactivity "> <com.fortysevendeg.swipelistview.swipelistview android:id=" @+id/example_lv_list "an Droid:layout_width= "Match_parent" android:layout_height= "match_parent" android:listselector= "#00000000" swipe:swipeactionleft= "Dismiss" swipe:swipeactionright= "reveal" swipe:swipeanimationtime= "0" swipe : swipebackview= "@+id/back" swipe:swipecloseallitemswhenmovelist= "true" swipe:swipefrontview= "@+id/front" Swipe:swipemode= "Both" swipe:swipeoffsetleft= "0DP" swipe:swipeoffsetright= "0DP" Swipe:swipeopenon Longpress= "false"/></relativelayout>
Here's a Swipelistview control, and I'm going to say a few hard-to-understand properties
Indicates the action when sliding, dismiss is deleted when sliding, if set to reveal indicates that the option after item is displayed when the slide is
swipe:swipeactionleft=" Dismiss "&NBSP;
swipe:swipeactionright=" reveal "&NBSP;
This is the ID of the back layout (we call the layout that we see directly in front, and the layout we see after sliding is called the back), The must correspond to the back layout id,
swipe:swipebackview= "@+id/back" &NBSP;
This is the layout of the back when scrolling, true to close, false to not close, general set to True &NBSP
Swipe:swipecloseallitemswhenmovelist= "true" &NBSP;
This is the ID of the previous layout, which corresponds to the ID of the layout
Swipe: swipefrontview= "@+id/front" &NBSP;
Both indicates that you can slide to the left or to the right.
swipe:swipemode= "both" &NBSP;
The following two offsets that slide left or right, typically not set in the XML file, but instead set the offset in code based on the size of the setting.
swipe:swipeoffsetleft= "0DP" &NBSP;
swipe:swipeoffsetright= "0DP" &NBSP;
take a look at the item layout file, which includes the front and back, Two overlapping:
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "> <!-- Layouts in LinearLayout are hidden after each item--<linearlayout android:id= "@+id/back" android:layout_width= "Match_parent" android:layout_height= "80DP" android:background= "#eee" android:tag= "Back" > <button android:id= "@+id/example _row_b_action_1 "android:layout_width=" 0DP "android:layout_height=" 60DP "android:layout_gravity=" Center "android: layout_marginright= "10DP" android:layout_weight= "1" android:text= "test"/> <button android:id= "@+id/example_ro W_b_action_2 "android:layout_width=" 0DP "android:layout_height=" 60DP "android:layout_gravity=" Center "android: layout_marginleft= "10DP" android:layout_weight= "1" android:text= "delete/> <button android:id=" @+id/example_row _b_action_3 "android:layout_width=" 0DP "android:layout_height=" 60DP "Android:layout_gravity= "Center" android:layout_weight= "1" android:text= "edit"/> </LinearLayout> <!--Here is the layout shown in the foreground--< Relativelayout android:id= "@+id/front" android:layout_width= "match_parent" android:layout_height= "80DP" Android: Background= "#ffffff" android:orientation= "vertical" android:tag= "front" > <textview android:id= "@+id/example _row_tv_title "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_ Centerinparent= "true" android:textsize= "18sp"/> </RelativeLayout></FrameLayout>
This layout is a regular layout and I won't explain it.
Mainactivity.java, there are notes in the key places.
public class Mainactivity extends Activity {private Swipelistview mswipelistview; Private Swipeadapter Madapter; public static int devicewidth; Private list<string> TestData; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mswipelistview = (Swipelistview) Findviewbyid (r.id.example_lv_list); TestData = Gettestdata (); Data adaptation Madapter = new Swipeadapter (this, R.layout.package_row, Testdata,mswipelistview); Get the device width devicewidth = getdevicewidth (); Mswipelistview.setadapter (Madapter); Set up Event listener Mswipelistview.setswipelistviewlistener (New Testbaseswipelistviewlistener ()); Reload (); } private list<string> Gettestdata () {String [] obj = new string[]{"Red Mansions", "Journey to the Monkey", "Outlaws of the Marsh", "tube cone", "notes of the song", "Kingdoms", "Advanced Programming for Android development", "Red Mansions", "Journey to the Monkey", "Outlaws of the Marsh", "tube Cone", "song Anthology Note", "Kingdoms", "Android Development Advanced Programming"}; List<string> list = new arraylist<string> (arrays.aslist (obj)); return list; } private int Getdevicewidth () {return getresources (). Getdisplaymetrics (). Widthpixels; } private void Reload () {//Mswipelistview.setswipemode (Swipelistview.swipe_mode_left);// Mswipelistview.setswipeactionleft (swipelistview.swipe_action_reveal);//Mswipelistview.setswipeactionright ( Settings.getswipeactionright ()); Swipe left offset, depending on the size of the device to determine the size of the offset mswipelistview.setoffsetleft (devicewidth * 1/3); Mswipelistview.setoffsetright (Devicewidth * 1/3);//Mswipelistview.setoffsetright (Convertdptopixel ( Settings.getswipeoffsetright ())); Set animation time Mswipelistview.setanimationtime (30); Mswipelistview.setswipeopenonlongpress (FALSE); } class Testbaseswipelistviewlistener extends baseswipelistviewlistener{//Click Response event for each item @Override PU Blic void Onclickfrontview (int position) {Super.onclickfrontview (position); ToasT.maketext (Getapplicationcontext (), testdata.get (position), Toast.length_short). Show (); }//Shutdown event @Override public void Ondismiss (int[] reversesortedpositions) {for (int position : reversesortedpositions) {log.i ("Lenve", "position--:" +position); Testdata.remove (position); } madapter.notifydatasetchanged (); } }}
data 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) {//Close animation mswipelistview.closeanimate (position); Call the dismiss method to delete the item (this method is in mainactivity) Mswipelistview.dismiss (position); } }); String item = getItem (position); Holder.mFrontText.setText (item); return convertview; } class viewholder{TextView mfronttext; Button Mbackedit,mbackdelete; }}
Swipelistview swipe to remove Android