Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

android - Drag and drop for linearlayout's child views

I have layout with 5 different child views, Child views are relativelayout with number of child views inside it, and all are different too. Hence i am using scrollview as root container like this:

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

I want to have drag and drop effect LinearLayout container. Any idea how to achieve Drag and Drop for LinearLayout container? I got working example for ListView, I guess for scrollview it should be same too, but scrollview can contain only one child view inside it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add this to your onCreate:

 yourView.setOnTouchListener(new OnTouchListener() {

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

And this to your Activity Class outside any methods.

       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;
               }
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...