ダッシュボードのボタンを長押しすると、同じ画面の任意の位置にドラッグ アンド ドロップしたいのですが、その変更またはビューの変更は今後の修正となります。現在、ボタンを特定の位置にドラッグできますが、他のビューから戻ると、そのビューが変化します。
これが私のコードです:
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;
}
});
これはボタンをドラッグする場合にのみ機能しますが、それらの位置を永続的に固定するわけではありません