【问题标题】:Drag and drop for linearlayout's child views拖放线性布局的子视图
【发布时间】:2012-05-31 13:18:34
【问题描述】:

我的布局有 5 个不同的子视图,子视图是相对布局,其中包含多个子视图,而且它们也各不相同。因此我使用滚动视图作为这样的根容器:

<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"
        android:fillViewport="true" android:scrollbars="none">

        <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
            android:id="@+id/home_page_scrollview_outer_layout">
         <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_alignParentLeft="true"
            android:background="@drawable/homescreen_yellow" >



            <ImageButton android:id="@+id/hp_imgbtn_listA" android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_alignParentRight="true" android:src="@drawable/arrow_right">
            </ImageButton>    

            <ListView
                android:id="@+id/listA"
                android:layout_below="@id/hp_txt_listA"
                android:background="@color/home_page_bg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </ListView>
     </RelativeLayout>

    <!-- List B layout -->    

        <RelativeLayout     
            android:layout_weight="1"
            android:layout_marginRight="10dip"
            android:background="@drawable/homescreen_cyan"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">


        </RelativeLayout>

     <!-- List C layout -->   

        <RelativeLayout     
            android:layout_weight="1"
            android:layout_marginRight="10dip"
            android:background="@drawable/homescreen_purple"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">

    </RelativeLayout>  

     <!-- List D layout -->   

        <RelativeLayout 
            android:layout_weight="1"   
            android:layout_marginRight="10dip"
            android:background="@drawable/homescreen_turqoise"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">

    </RelativeLayout> 

     <!-- List E layout -->   

        <RelativeLayout 
            android:layout_weight="1"   
            android:layout_marginRight="10dip"
            android:background="@drawable/homescreen_turqoise"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">

    </RelativeLayout> 
    <LinearLayout>
</ScrollView>   

我想要有拖拽效果的LinearLayout容器。知道如何实现LinearLayout容器的拖拽吗?我有 ListView 的工作示例,我想滚动视图也应该是一样的,但滚动视图中只能包含一个子视图。

【问题讨论】:

    标签: android android-layout drag-and-drop scrollview


    【解决方案1】:

    将此添加到您的 onCreate:

     yourView.setOnTouchListener(new OnTouchListener() {
    
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                drag(event, v);
                return false;
            }
        });
    

    这在任何方法之外都适用于您的活动类。

           public void drag(MotionEvent event, View v)
            {
    
                RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();
    
                switch(event.getAction())
                {
                   case MotionEvent.ACTION_MOVE:
                   {
                     params.topMargin = (int)event.getRawY() - (v.getHeight());
                     params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                     v.setLayoutParams(params);
                     break;
                   }
                   case MotionEvent.ACTION_UP:
                   {
                     params.topMargin = (int)event.getRawY() - (v.getHeight());
                     params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                     v.setLayoutParams(params);
                     break;
                   }
                   case MotionEvent.ACTION_DOWN:
                   {
                    v.setLayoutParams(params);
                    break;
                   }
                }
    

    【讨论】:

    • 感谢您的回答,但我已经使用 Android 3.0 Drag N Drop 框架实现了。但它向我展示了简单的拖放无法满足我的要求的路径。我还需要在视图组及其包含的子视图上拖放。
    • 我想你可以将它添加到任何东西的 ontouch 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2014-12-17
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多