Android перетаскивание виджета

В настоящее время я работаю над простой пользовательской программой запуска Android, и у меня есть проблема с управлением виджетами. Я пытаюсь перетащить виджет, выбранный пользователем. Вот мой код

public class FragmentBlue extends Fragment implements OnLongClickListener 
{
static final String TAG = "FragmentBlue";
private static final String IMAGEVIEW_TAG = "The Android Logo";
public static final int RESULT_OK = -1;
public static final int CODE_PICK_APPWIDGET = 1;
public static final int RESULT_CANCELED = 0;

Object DragShadowBuilder;

AppWidgetManager mAppWidgetManager;
LauncherAppWidgetHost mAppWidgetHost;   
ViewGroup mainlayout; 
int numwidgets;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_blue, container, false);    
    mainlayout = (ViewGroup) view.findViewById(R.id.home_view);    
    mainlayout.setOnLongClickListener(this);

    mAppWidgetManager = AppWidgetManager.getInstance(getActivity());
    mAppWidgetHost = new LauncherAppWidgetHost(getActivity(), R.id.APPWIDGET_HOST_ID);
    return view;
}

/**
 * Launches the menu to select the widget. The selected widget will be on
 * the result of the activity.
 */
void selectWidget() {       
    int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    addEmptyData(pickIntent);
    startActivityForResult(pickIntent, CODE_PICK_APPWIDGET);
}    

void addEmptyData(Intent pickIntent) {
    ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
    pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
    pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
}

/**
 * If the user has selected an widget, the result will be in the 'data' when
 * this function is called.
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == CODE_PICK_APPWIDGET) {
            configureWidget(data);
        } else if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
            createWidget(data);
        }
    } else if (resultCode == RESULT_CANCELED && data != null) {
        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        if (appWidgetId != -1) {
            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        }
    }
}

/**
 * Checks if the widget needs any configuration. If it needs, launches the
 * configuration activity.
 */
private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    if (appWidgetInfo.configure != null) {
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidgetInfo.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
    } else {
        createWidget(data);
    }
}

/**
 * Creates the widget and adds to our view layout.
 */
public void createWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

    LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) mAppWidgetHost.createView(getActivity(), appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mainlayout.getWidth()/3, mainlayout.getHeight()/3);
    lp.leftMargin = numwidgets * (mainlayout.getWidth()/3);

    hostView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
              } else {
                return false;
              }

        }
    });
    hostView.setOnDragListener(new OnDragListener() {
         Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
            Drawable normalShape = getResources().getDrawable(R.drawable.shape);
        @Override
        public boolean onDrag(View v, DragEvent event) {
             int action = event.getAction();
              switch (event.getAction()) {
              case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;
              case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackgroundDrawable(enterShape);
                break;
              case DragEvent.ACTION_DRAG_EXITED:
                v.setBackgroundDrawable(normalShape);
                break;
              case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                View view = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                GridLayout container = (GridLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
              case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(normalShape);
              default:
                break;
              }
              return true;
        }
    });


    mainlayout.addView(hostView,lp);
    numwidgets ++;
}

Я использую xml:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    android:columnWidth="300dp"
    android:orientation="vertical"
    android:stretchMode="columnWidth"   
    android:id="@+id/home_view"
    >
    <android.widget.Space
        android:id="@+id/space1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</GridLayout>

Проблема в том, когда я начинаю перетаскивать виджет. Я могу перетащить его, но когда я пытаюсь его бросить, он исчезает. я предполагаю, что это причина моих макетов, но я не могу найти никакого решения... Я знаю, что не совсем понимаю, но я новичок в мире Android. Все равно спасибо!!!


person Tzeik    schedule 07.07.2014    source источник
comment
Пока нет, но я оставил эту часть Android на данный момент.   -  person Tzeik    schedule 09.09.2014


Ответы (2)


Не виджеты должны реализовывать DragListener, а те места, куда их можно поместить.

Попробуй

mainlayout = (ViewGroup) view.findViewById(R.id.home_view);    
mainlayout.setOnDragListener(new OnDragListener() {
         Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
            Drawable normalShape = getResources().getDrawable(R.drawable.shape);
        @Override
        public boolean onDrag(View v, DragEvent event) {
             int action = event.getAction();
              switch (event.getAction()) {
              case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;
              case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackgroundDrawable(enterShape);
                break;
              case DragEvent.ACTION_DRAG_EXITED:
                v.setBackgroundDrawable(normalShape);
                break;
              case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                View view = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                GridLayout container = (GridLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
              case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(normalShape);
              default:
                break;
              }
              return true;
        }
    });
person user3430340    schedule 06.10.2014

Вы должны установить OnDragListener как на View, куда вы хотите перетащить, так и на View, куда вы хотите перетащить.

person skywall    schedule 06.10.2014