How To Make A Swipe To Delete ListView


Step1: Download SwipeMenuListView.zip

Step2: Paste it in your Project.

Step3: Add in Xml


<android.com.indianwedding.swipemenulistview.SwipeMenuListView
            android:listSelector="@android:color/transparent"
            android:id="@+id/order_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


Step4: Add in your java file

(add items)
SwipeMenuCreator creator = new SwipeMenuCreator() {

    @Override
    public void create(SwipeMenu menu) {
        // create "open" item
        SwipeMenuItem openItem = new SwipeMenuItem(
                getApplicationContext());
        // set item background
        openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                0xCE)));
        // set item width
        openItem.setWidth(dp2px(90));
        // set item title
        openItem.setTitle("Open");
        // set item title fontsize
        openItem.setTitleSize(18);
        // set item title font color
        openItem.setTitleColor(Color.WHITE);
        // add to menu
        menu.addMenuItem(openItem);

        // create "delete" item
        SwipeMenuItem deleteItem = new SwipeMenuItem(
                getApplicationContext());
        // set item background
        deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                0x3F, 0x25)));
        // set item width
        deleteItem.setWidth(dp2px(90));
        // set a icon
        deleteItem.setIcon(R.drawable.ic_delete);
        // add to menu
        menu.addMenuItem(deleteItem);
    }
};

// set creator
listView.setMenuCreator(creator);


(listener item click event)

listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
        switch (index) {
        case 0:
            // open
            break;
        case 1:
            // delete
            break;
        }
        // false : close the menu; true : not close the menu
        return false;
    }
});

(Swipe Direction)
// Right mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_RIGHT); // Left mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);

(Create Diff. Menu)
  • Use the ViewType of adapter
    class AppAdapter extends BaseAdapter {

        ...

        @Override
        public int getViewTypeCount() {
            // menu type count
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            // current menu type
            return type;
        }

        ...
    }
  • Create different menus depending on the view type
    SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // Create different menus depending on the view type
                switch (menu.getViewType()) {
                case 0:
                    // create menu of type 0
                    break;
                case 1:
                    // create menu of type 1
                    break;
                ...
                }
            }

        };

Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment