【开源】DragLinearLayout

DragLinearLayout

  •  https://github.com/justasm/DragLinearLayout

    介绍:

    可以拖拽排序的LinearLayout。其实拖拽排序已经有很多ListView版本的实现,为什么还需要LinearLayout里面拖拽排序呢?因为LinearLayout的使用比ListView简单,如果你只有几个Item,使用ListView是不是太浪费?

    运行效果:

 

使用说明:

DragLinearLayout可以用来替换LinearLayout,默认子view是不可拖动的,需要调用DragLinearLayout#setViewDraggable(View, View)来让某个子view可拖动。

XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<com.jmedeisis.draglinearlayout.DragLinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        android:src="@drawable/image"/>
 
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_text"/>
 
</com.jmedeisis.draglinearlayout.DragLinearLayout>

让所有子view可以拖动:

1
2
3
4
5
6
DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container);
for(int i = 0; i < dragLinearLayout.getChildCount(); i++){
    View child = dragLinearLayout.getChildAt(i);
    // the child will act as its own drag handle
    dragLinearLayout.setViewDraggable(child, child);
}

可以动态添加可拖动子view

1
2
3
4
5
6
final View view = View.inflate(context, R.layout.view_layout, null);
dragLinearLayout.addDragView(view, view.findViewById(R.id.view_drag_handle));
 
// ..
 
dragLinearLayout.removeDragView(view);

 

使用OnViewSwapListener检测子view之间的排序变化事件:

1
2
3
4
5
6
7
dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {
    @Override
    public void onSwap(View firstView, int firstPosition,
            View secondView, int secondPosition) {
        // update data, etc..
    }
});

当在ScrollView中使用DragLinearLayout的时候,如果你想在拖拽的时候ScrollView也能滚动,需要调用

1
setContainerScrollView(ScrollView)

为了达到最佳效果,最好给子view加上不透明的背景色。不要给DragLinearLayout设置padding,如果需要padding,加在子view上。

 

限制

只支持垂直布局。

需要依赖NineOldAndroids库。

posted on 2015-04-06 10:59  wasdchenhao  阅读(523)  评论(0)    收藏  举报

导航