SwipeListView实现仿ios的侧滑详解手机开发

github地址:https://github.com/xiangzhihong/SwipeMenuListView

今天介绍一个SwipeMenuListView实现侧滑删除的例子,其实和listview的用法一样,就是多了创建删除等view的步骤,然后通过addview添加到父布局中。

效果如果:

SwipeListView实现仿ios的侧滑详解手机开发

当然你也也可以根据自己的需要增加更多的自定义View。

SwipeListView实现仿ios的侧滑详解手机开发

直接看代码吧:

Add dependency

dependencies { 
    compile 'com.baoyz.swipemenulistview:library:1.3.0' 
}

Step 1

  • add SwipeMenuListView in layout xml
<com.baoyz.swipemenulistview.SwipeMenuListView 
        android:id="@+id/listView" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" />

Step 2

  • create a SwipeMenuCreator to 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);

Step 3

  • 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 directions

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

Create Different 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; 
                ... 
                } 
            } 
 
        };

大家可以把代码fork下来,自行修改优化

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5666.html

(0)
上一篇 2021年7月17日
下一篇 2021年7月17日

相关推荐

发表回复

登录后才能评论