Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Swipe to delete to ListView items in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.76k
    Comment on it

    In many situations, we need to add swipe menu functionality to make our listview attractive.

    Here I am using a library that makes it so simple.

    Firstly, we need to add a dependency to gradle file

    compile 'com.baoyz.swipemenulistview:library:1.3.0'

    Then use list of the library

     <com.baoyz.swipemenulistview.SwipeMenuListView
            android:id="@+id/jobsList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    

    Here I am creating two item, one for delete the listview item and other is for open the item.
    Here I am defining left direction swipe by using this setSwipeDirection() method.

    SwipeMenuCreator swipeCreator = new SwipeMenuCreator() { @Override public void create(SwipeMenu menu) { // create "open" item SwipeMenuItem opItem = new SwipeMenuItem( getApplicationContext()); // set item background opItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE))); opItem.setWidth((250)); opItem.setTitle("Open"); opItem.setTitleSize(20); opItem.setTitleColor(Color.WHITE); menu.addMenuItem(opItem); SwipeMenuItem delItem = new SwipeMenuItem( getApplicationContext()); delItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE))); delItem.setTitleSize(20); deleteItem.setBackground(R.color.delete_color); delItem.setWidth((250));

    delItem.setTitleColor(Color.WHITE); delItem.setTitle("Delete"); menu.addMenuItem(delItem); } }; // set creator jobsList.setMenuCreator(swipeCreator); jobsList.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT); jobsList.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { switch (index) { case 0: // open Toast.makeText(getApplicationContext(), "Index: " + index + " " + "Position: " + position, Toast.LENGTH_LONG).show(); //Toast.makeText(CustomerListActivity.this,"Position: "+index,Toast.LENGTH_LONG).show(); break; case 1: // delete Toast.makeText(getApplicationContext(), "Index: " + index + " " + "Position: " + position, Toast.LENGTH_LONG).show(); break; } return false; } });

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: