1

在长按仪表板按钮时,我想在同一屏幕的任何位置执行拖放操作,并且更改或视图更改成为未来的修复。现在我可以将按钮拖动到特定位置,但是当我们从其他视图返回时,该视图会发生变化。
这是我的代码:
fragment_dashboard.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_new"
android:screenOrientation="portrait" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/home_btn_cafapp"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/ic_home_cafapp_1"
        android:text="CAF Application"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_smartinfo"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/ic_home_smartinfo_1"
        android:text="SmartInfo"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_reports"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/ic_home_reports_1"
        android:text="SmartReport"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_stock"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/ic_home_requisition_1"
        android:text="Stock Requisition"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_prepaid"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/ic_home_newcaf_1"
        android:text="CAF New Flow"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_setting"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/ic_logs"
        android:text="Settings"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_todos"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/i"
        android:text="Instructions"
        android:visibility="visible"
        android:textColor="@color/light_green" />

    <Button
        android:id="@+id/home_btn_crm_tracking"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/h" />

    <Button
        android:id="@+id/home_btn_faqs"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/j"
        android:visibility="invisible" />

    <Button
        android:id="@+id/home_btn_todos"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/i"
        android:visibility="invisible" />

    <Button
        android:id="@+id/home_btn_todos"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/i"
        android:visibility="invisible" />

    <Button
        android:id="@+id/home_btn_faqs"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/j"
        android:visibility="invisible" />

    <Button
        android:id="@+id/home_btn_update_app"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/home_btn_cafapp"
        style="@style/DashboardButton"
        android:layout_marginLeft="50dp"
        android:drawableTop="@drawable/getupdate"
        android:text="Update Application"
        android:textColor="@color/light_green"
        android:visibility="gone" />

    </LinearLayout></LinearLayout>

dashboard_fragment.java在这里:

//onlong click method-
public boolean onLongClick(View v)
{
    dragged_view = v;
    v.setVisibility(View.INVISIBLE);
    v.startDrag(null, new DragShadowBuilder(v), v, 0);
    return true;
    }

draglistener:

linearLayout.setOnDragListener(new View.OnDragListener()
    {
        public boolean onDrag(View v, DragEvent event)
        {
            if (event.getLocalState() == v)
                return true;
            View view = (View) event.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            switch (event.getAction())
            {
                case DragEvent.ACTION_DRAG_STARTED:
                    // v.setVisibility(View.INVISIBLE);
                    v.invalidate();
                    return true;
                case DragEvent.ACTION_DRAG_LOCATION:
                    return (true);

                case DragEvent.ACTION_DRAG_EXITED:
                    v.invalidate();
                    return false;

                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup

                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    x = x / (v.getWidth() / 4);
                    y = (y / (v.getHeight() / 3));

                    owner.removeView(view);
                    owner.addView(view, x + y*4);
                    view.setVisibility(View.VISIBLE);
                    view.invalidate();

                    return true;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.invalidate();
                    if (event.getResult())
                    {
                        Toast.makeText(getActivity(), "The drop was handled.", Toast.LENGTH_LONG).show();

                    }
                    else
                    {
                        v.setVisibility(View.VISIBLE);
                        Toast.makeText(getActivity(), "The drop didn't work.", Toast.LENGTH_LONG).show();

                    }

                    return true;
                default:
                    break;
            }
            return true;
        }
    });

这仅适用于拖动按钮,但不能永久固定它们的位置

4

0 回答 0