Android ListView Sliding Delete operation (Swipelistview) _android

Source: Internet
Author: User

The new version of the micro-letter and QQ on the introduction of the sliding delete feature is now a more popular feature. In fact this sliding delete control, GitHub already has, is a popular open source framework Swipelistview. However, this swipelistview is a framelayout, that is, a two-storey layout, the above layout front cover the following layout back, sliding will slide open front, so that the following back is displayed. But looked at the micro-letter sliding deletion seems not to be like this, it is more than a single layer of the screen layout, sliding is the right beyond the screen button to enter the screen, guess should not use the Swipelistview control. QQ Sliding Delete is the item on the right side of the ListView hidden a button, but detected the slide event, give Button a show of animation, so that it can be seen, this scheme should be the best to achieve.

This article is mainly to learn Swipelistview this open source framework.

There are two ways to use this framework, one of which is to import the Swipelistviewlibrary project as a repository for Android engineering. Because the Swipelistviewlibrary library project itself relies on another popular open source framework Nineoldandroids, it is also easy to search online or github.

Import the two library projects, for Nineoldandroids, do the following settings, in fact, the main is to check is the library this option, so can be nineoldandroids engineering as a dependent library of other projects to use:

For Swipelistviewlibrary, in addition to checking the IS library option, remember to add inside the next, plus nineoldandroids as the library's dependent library:

The following is the use of this library, first clean up the top two library projects, many times the error of the project, it would be nice. Then create your own project and add the Swipelistviewlibrary project to the Add option. This allows you to directly use the Swipelistview control, which is simple, and the code is as follows:

 <com.fortysevendeg.swipelistview.swipelistview
  android:id= "@+id/swipe_lv"
  android:layout_width= " Match_parent "
  android:layout_height=" match_parent "
  app:swipemode=" left "
  app:swipeanimationtime=" "
  app:swipeoffsetleft=" 200DP "app:swipefrontview=" @+id/front "app:swipebackview="
  @+id/back "
  app:swipeactionleft= "reveal"/>

Where the App:swipefrontview attribute specifies that the id,app:swipebackview of the view above the framelayout above is specified as the ID of the view at the following level. In the following custom baseadatpter to use the layout of the item you can see:

 <?xml version= "1.0" encoding= "Utf-8"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <relativelayout androi
      D:id= "@+id/back" android:layout_width= "match_parent" android:layout_height= "match_parent" > <Button Android:id= "@+id/close_btn" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:layout_centervertical= "true" android:layout_toleftof= "@+id/delete_btn" android:text= "Close" Androi D:textappearance= "? Android:attr/textappearancemedium" android:focusable= "false" android:focusableintouchmode= " False "/> <button android:id=" @+id/delete_btn "android:layout_width=" Wrap_content "Android:layo" ut_height= "Wrap_content" android:layout_centervertical= "true" android:layout_alignparentright= "true" and roid:text= "Delete" Android:Textappearance= "? Android:attr/textappearancemedium" android:focusable= "false" android:focusableintouchmode= "FA LSE "/> </RelativeLayout> <relativelayout android:id=" @+id/front "android:layout_width=" Match_paren T "android:layout_height=" match_parent "android:background=" @android: Color/white "> <textview Andro Id:id= "@+id/content_tv" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android
      : gravity= "center" android:text= "Hello World" android:textappearance= "? Android:attr/textappearancemedium" Android:textcolor= "@android: Color/black"/> </RelativeLayout> </FrameLayout>

The code initialized in the activity:

arrays = new Arraylist<string> (arrays.aslist (util.arrays));

    Mswipelv = (Swipelistview) Findviewbyid (R.ID.SWIPE_LV);
    Madapter = new Myadapter (this, arrays);
    Mswipelv.setadapter (madapter);
    Mswipelv.setswipelistviewlistener (New Baseswipelistviewlistener () {

      @Override public
      void onclosed (int Position, Boolean fromright) {
      }
    });

and the GetView () in the custom baseadapter:

    @Override Public View getview (final int position, View Convertview, final viewgroup parent) {Viewholder ho

      Lder;
        if (Convertview = = null) {holder = new Viewholder ();
        Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.layout_swipe_list_item, NULL);
        Holder.mcontenttv = (TextView) Convertview.findviewbyid (R.ID.CONTENT_TV);
        HOLDER.MCLOSEBTN = (Button) Convertview.findviewbyid (R.ID.CLOSE_BTN);

        HOLDER.MDELETEBTN = (Button) Convertview.findviewbyid (R.ID.DELETE_BTN);
      Convertview.settag (holder);
      else {holder = (Viewholder) convertview.gettag ();
      } holder.mContentTv.setText (Arrays.get (position));
          Holder.mCloseBtn.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {
        ((Swipelistview) parent). Closeanimate (position);
      }
      });

        Holder.mDeleteBtn.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {(Swipelistview) parent). Closeopeneditems (); Mhandler.postdelayed (New Runnable () {@Override public void run () {Marrays.remove (
              position);
            Madapter.notifydatasetchanged ();
        }, 350);

      }
      });
    return convertview;

 }

And then OK, the effect of the operation of the project is as follows:

Another way is to use the Swipelistview control method is to directly import the official two jar package, the opening address can be seen, but the direct import of these two jar packages, does not mean that you can immediately use! First, add the package to the build path of the new project, If your project does not add Android Support Pack Android-support-v4.jar Remember to add the following, then remember the res\values\ from the Swipelistviewlibrary Library project that was previously imported The Swipelistview__attrs.xml file is copied to the res/values/directory of the New project, which is primarily a declaration of the properties within the Swipelistview control, and the directly imported jar package does not contain files that declare these attributes. And then just like the above quote in the code, but need to pay attention to two points: one, jar package inside Swipelistview package name and library project inside the package name is not the same, the reference need to pay attention to the following; Said there was no statement of Swipefrontview and Swipebackview two attributes, this question seems to be a bug,stackoverflow on the Swipelistview framework that someone has pointed out, The idea is to declare in the layout file Swipefrontview and Swipebackview These two attributes are worth the time, it is best not to customize the name of the ID, but to use the Swipelistview_backview and Swipelistview_ Frontview.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.