The use of Android development Swipelistview

Source: Internet
Author: User

Implement a feature similar to the sliding list in QQ:

Swipe left or right, and then perform the related action.

Here is the open source control Swipelistview,https://github.com/47deg/android-swipelistview on GitHub, and after it's downloaded, we can refer to the download as a library file, Of course, you can also directly copy the source code to our thinking wood. Swipelistview also relies on a third-party control on GitHub, called Nineoldandroids,https://github.com/jakewharton/nineoldandroids, Nineoldandroids and Swipelistview, as well, can be copied directly into the code or as a library file to reference. This case unifies them all as a library file to reference.

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. SwipelistviewAndroid:id="@+id/example_lv_list"Android: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:swipeopenonlongpress="false"/></relativelayout>

Here's a Swipelistview control, I say a few hard-to-understand properties

Represents the action when sliding, dismiss is deleted when sliding, if set to reveal indicates that the option after item is displayed when sliding
swipe:swipeactionleft= "Dismiss"
Swipe:swipeactionright= "Reveal"
This is the ID of the back layout (we call the layout that we see directly in front, the layout after sliding to see the back) and must correspond to the back layout ID
swipe:swipebackview= "@+id/back"
This is when scrolling is turned off on the back of the layout, true indicates off, false means not closed, generally set to True
Swipe:swipecloseallitemswhenmovelist= "true"
This is the ID of the previous layout, which corresponds to the ID of the layout
swipe:swipefrontview= "@+id/front"
Both indicates that you can slide to the left or to the right, and the left and right indicate that you can slide only to or from there.
Swipe:swipemode= "both"
The following two offsets, which are typically not set in the XML file, are offset to the left or right, and instead are set in code based on the size of the setting.
swipe:swipeoffsetleft= "0DP"
swipe:swipeoffsetright= "0DP"
Take a look at the item layout file, which includes the front and back, with 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" >            <!--layout in LinearLayout is the layout that is hidden after each item--    <linearlayout  android:id  = "@+id/back"  android:layout_width  =< Span class= "Hljs-value" > "match_parent"  android:layout_height  = "80DP"  android:background  =         "#eee"  android:tag  = "back" ;         <button  android:id= "@+id/example_row_b_action_1"  android:layout_wid  th  = "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_row_b_action_2"  android:layout_wid  th  = "0DP"  android:layout_height  = "60DP"  android:layout_gravity  =" center " android:layout_marginleft  ="            10DP " android:layout_weight  =" 1 " android:text  = "delete" />         <buttonandroid: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 at the front desk--    <relativelayout  android: ID  = "@+id/front"  android:layout_width  = "match_parent"  android:layout_height  = "80DP"  android:background  =         "#ffffff"  android:orientation  = "vertical"  android:tag  =;         <TextViewandroid: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 {    PrivateSwipelistview Mswipelistview;PrivateSwipeadapter Madapter; Public Static intDevicewidth;PrivateList<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 adaptationMadapter =NewSwipeadapter ( This, R.layout.package_row, Testdata,mswipelistview);//Get the device widthDevicewidth = Getdevicewidth (); Mswipelistview.setadapter (Madapter);//Set Event listenerMswipelistview.setswipelistviewlistener (NewTestbaseswipelistviewlistener ());    Reload (); }PrivateList<string>Gettestdata() {String [] obj =Newstring[]{"Red Mansions","Journey to the Monkey","Outlaws of the Marsh","Pipe Cone series","Notes of the song anthology","Kingdoms","Advanced Programming for Android development","Red Mansions","Journey to the Monkey","Outlaws of the Marsh","Pipe Cone series","Notes of the song anthology","Kingdoms","Advanced Programming for Android development"}; list<string> list =NewArraylist<string> (arrays.aslist (obj));returnList }Private int Getdevicewidth() {returnGetresources (). 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 offsetMswipelistview.setoffsetleft (Devicewidth *1/3); Mswipelistview.setoffsetright (Devicewidth *1/3);//Mswipelistview.setoffsetright (Convertdptopixel (Settings.getswipeoffsetright ()));        //Set animation timeMswipelistview.setanimationtime ( -); Mswipelistview.setswipeopenonlongpress (false); } class Testbaseswipelistviewlistener extends baseswipelistviewlistener{//Click Response events for each item        @Override         Public void Onclickfrontview(intPosition) {Super. Onclickfrontview (position);        Toast.maketext (Getapplicationcontext (), testdata.get (position), Toast.length_short). Show (); }//Close event        @Override         Public void Ondismiss(int[] reversesortedpositions) { for(intPosition:reversesortedpositions) {LOG.I ("Lenve","position--:"+position);            Testdata.remove (position);        } madapter.notifydatasetchanged (); }    }}

Data adapter:

 Public  class swipeadapter extends arrayadapter<String> {    PrivateLayoutinflater Minflater;PrivateList<string> objects;PrivateSwipelistview Mswipelistview; PublicSwipeadapter (Context context,inttextviewresourceid,list<string> objects, Swipelistview mswipelistview) {Super(Context, Textviewresourceid, objects); This. objects = objects; This. Mswipelistview = Mswipelistview;    Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); } @Override PublicView GetView (Final intPosition, View Convertview, ViewGroup parent) {Viewholder Holder =NULL;if(Convertview = =NULL) {Convertview = Minflater.inflate (R.layout.package_row, parent,false); Holder =NewViewholder ();            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 (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Close animationMswipelistview.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);returnConvertview; } class viewholder{TextView Mfronttext;    Button Mbackedit,mbackdelete; }}

This project complete code download Http://pan.baidu.com/s/1o6knuUA

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

The use of Android development Swipelistview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.