2012-05-31 84 views
2

我有5個不同的子視圖的佈局,子視圖與其中的子視圖的數量相關,並且都是不同的。因此我使用滾動型像這根容器:拖放linearlayout的子視圖

<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的工作示例,我想scrollview它應該也是一樣的,但scrollview中只能包含一個子視圖。

回答

2

添加到您的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; 
       } 
      } 
+0

感謝您的回答,但我已經實現了Android 3.0 Drag N Drop框架。但它向我展示了我的需求不能通過簡單的拖放來實現的路徑。我還需要拖放viewgroup及其包含的子視圖。 – Zoombie

+0

我想你可以把它添加到任何東西的ontouch。 –