android listview 移动,SlideAndDragListView

SlideAndDragListView

download.svg:license-apache-blue.svgSlideAndDragListView.svg?branch=masterAPI-11%2B-brightgreen.svg?style=flatMethods%20count-287-e91e63.svgSize-29%20KB-e91e63.svg

A ListView that you can slide ( or swipe ) the items, drag and drop the items to other places.

10eaab5d4f32baae40c25cd3aeb76af1.png

ChangeLog: CHINESE.md

Overview

SlideAndDragListView (SDLV) is an extension of the Android ListView that enables slide and drag-and-drop reordering of list items.

Some key features are:

Clean drag and drop.

Intuitive and smooth scrolling while dragging or sliding.

Support onItemClick and onItemLongClick listener.

Public callback methods.

Two side of item can slide.

so on...

SlideAndDragListView is useful for all kinds of prioritized lists: favorites, playlists, checklists, etc. Would love to hear about your use case or app by email. I hope you find it useful; and please, help me improve the thing!

Binaries

Gradle

compile 'com.yydcdut.sdlv:sdlv:0.5.4@aar'

Or

compile 'com.yydcdut.sdlv:sdlv:0.5.4'

aar

Jar

Widget Usage

Menu Item Click & Slide Directions

Step 1

Add SlideAndDragListView in layout xml

android:layout_width="fill_parent"

android:layout_height="fill_parent">

Step 2

Create a Menu and add MenuItem

Menu menu = new Menu(true, true, 0);//the second parameter is whether can slide over

menu.addItem(new MenuItem.Builder().setWidth(90)//set Width

.setBackground(new ColorDrawable(Color.RED))// set background

.setText("One")//set text string

.setTextColor(Color.GRAY)//set text color

.setTextSize(20)//set text size

.build());

menu.addItem(new MenuItem.Builder().setWidth(120)

.setBackground(new ColorDrawable(Color.BLACK))

.setDirection(MenuItem.DIRECTION_RIGHT)//set direction (default DIRECTION_LEFT)

.setIcon(getResources().getDrawable(R.drawable.ic_launcher))// set icon

.build());

//set in sdlv

slideAndDragListView.setMenu(menu);

The class Menu, the construct function Menu(bool wannaTransparentWhileDragging, boolean wannaOver, int menuViewType), the first parameter means whether set background transparent while dragging, the second parameter means whether can slide over.

If it’s true:

93a5a2fae92c7b41a7683d8259f48e57.gif

If it’s false:

33e744d5ac9e7bdaf4ee13e01e592d81.gif

The third parameter stands for view type, the value of int getItemViewType(int ) in BaseAdapter.

Step 3

Implement menu item click listener

slideAndDragListView.setOnSlideListener(new SlideAndDragListView.OnSlideListener() {

@Override

public void onSlideOpen(View view, View parentView, int position, int direction) {

}

@Override

public void onSlideClose(View view, View parentView, int position, int direction) {

}

});

slideAndDragListView.setOnMenuItemClickListener(new SlideAndDragListView.OnMenuItemClickListener() {

@Override

public int onMenuItemClick(View v, int itemPosition, int buttonPosition, int direction) {

switch (direction) {

case MenuItem.DIRECTION_LEFT:

switch (buttonPosition) {

case 0://One

return Menu.ITEM_SCROLL_BACK;

}

break;

case MenuItem.DIRECTION_RIGHT:

switch (buttonPosition) {

case 0://icon

return Menu.ITEM_DELETE_FROM_BOTTOM_TO_TOP;

}

break;

default :

return Menu.ITEM_NOTHING;

}

}

});

Have to set OnSlideListener!!!!!!

Menu.ITEM_NOTHING:

6dbbe7ffdabe9821b1a4eaa21e52538a.gif

Menu.ITEM_SCROLL_BACK:

4ac296c74c0a2da0923cd317a2578db5.gif

Menu.ITEM_DELETE_FROM_BOTTOM_TO_TOP:

3bc726d948c2c1ba52f63d44ec8f3a55.gif

Create Different Menu

Use the ViewType of adapter

private BaseAdapter mAdapter = new BaseAdapter() {

// .......

@Override

public int getItemViewType(int position) {

return position % 2;//current menu type

}

@Override

public int getViewTypeCount() {

return 2;//menu type count

}

// ......

}

Create different menus depending on the view type

List

Menu menu0 = new Menu(new ColorDrawable(Color.WHITE), true, 0);

menu0.addItem(new MenuItem.Builder().setWidth(90)//set Width

.setBackground(new ColorDrawable(Color.RED))// set background

.setText("One")//set text string

.setTextColor(Color.GRAY)//set text color

.setTextSize(20)//set text color

.build());

menu0.addItem(new MenuItem.Builder().setWidth(120)

.setBackground(new ColorDrawable(Color.BLACK))

.setDirection(MenuItem.DIRECTION_RIGHT)//set direction (default DIRECTION_LEFT)

.setIcon(getResources().getDrawable(R.drawable.ic_launcher))// set icon

.build());

Menu menu1 = new Menu(new ColorDrawable(Color.YELLOW), false, 1);

menu1.addItem(new MenuItem.Builder().setWidth(60)

.setBackground(new ColorDrawable(Color.RED))

.setText("Two")

.setTextColor(Color.GRAY)

.setTextSize(25)

.build());

menu1.addItem(new MenuItem.Builder().setWidth(70)

.setBackground(new ColorDrawable(Color.BLUE))

.setText("Three")

.setDirection(MenuItem.DIRECTION_RIGHT)

.setTextColor(Color.BLACK)

.setTextSize(20)

.build());

menuList.add(menu0);

menuList.add(menu1);

listView.setMenu(menuList)

See the demo

d3bd36e4d81d7c028b76e444a545fae7.gif

Drag

slideAndDragListView.setOnDragListener(new SlideAndDragListView.OnDragListener() {

@Override

public void onDragViewStart(int position) {

}

@Override

public void onDragViewMoving(int position) {

}

@Override

public void onDragViewDown(int position) {

}

}, mDataList);

public void onDragViewStart(int position).The parameter position is the position in ListView where dragged from.

public void onDragViewMoving(int position) .The parameter position is the position in ListView where dragged from, and this method will be called while the dragged item moving, as the same time, the position is changing.

public void onDragViewDown(int position) . The parameter position is the position in ListView where dropped down.

Other Listeners

ListView Item Click Listener

slideAndDragListView.setOnListItemClickListener(new SlideAndDragListView.OnListItemClickListener() {

@Override

public void onListItemClick(View v, int position) {

}

});

public void onListItemClick(View view, int position) . The parameter view is the ListView item that is clicked, and the parameter position is the position of the view in the list.

ListView Item Long Click Listener

slideAndDragListView.setOnListItemLongClickListener(new SlideAndDragListView.OnListItemLongClickListener() {

@Override

public void onListItemLongClick(View view, int position) {

}

});

public void onListItemLongClick(View view, int position) . The parameter view is the ListView item which is long clicked, and the parameter position is the position of the view in the list.

Item Slide Listener

slideAndDragListView.OnSlideListener() {

@Override

public void onSlideOpen(View view, View parentView, int position, int direction) {

}

@Override

public void onSlideClose(View view, View parentView, int position, int direction) {

}

});

public void onSlideOpen(View view, View parentView, int position, int direction). The parameter view is the ListView item that is slide open, parentView here is SDLV, position is the position of the view in the list, and the parameter direction is the item slided direction.

public void onSlideClose(View view, View parentView, int position, int direction). The parameter view is the ListView item that is slide close,parentView here is SDLV, position is the position of the view in the list, and the parameter direction is the item slided direction.

Item Delete Listener

slideAndDragListView.setOnItemDeleteListener(new SlideAndDragListView.OnItemDeleteListener() {

@Override

public void onItemDelete(View view, int position) {

}

});

public void onItemDelete(View view, int position) will invoked after int onMenuItemClick(View v, int itemPosition, int buttonPosition, int direction) return Menu.ITEM_DELETE_FROM_BOTTOM_TO_TOP.

Scroll Listener

slideAndDragListView.setOnListScrollListener(new SlideAndDragListView.OnListScrollListener(){

@Override

public void onScrollStateChanged(AbsListView view,int scrollState){

if(scrollState==SlideAndDragListView.OnListScrollListener.SCROLL_STATE_FLING){

}else if(scrollState==SlideAndDragListView.OnListScrollListener.SCROLL_STATE_FLING){

}else if(scrollState==SlideAndDragListView.OnListScrollListener.SCROLL_STATE_TOUCH_SCROLL){

}

}

@Override

public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount){

}

});

Same as ListView.OnScrollListener .

API

Close Menu

slideAndDragListView.closeSlidedItem();

Close menu manually.

License

Copyright 2015 yydcdut

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值