2014-07-07 85 views
0

我目前正在研究一個簡單的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世界。無論如何謝謝!

+0

你解決了嗎? –

+0

還沒有,但我暫時離開了這部分機器人。 – Tzeik

回答

0

小部件不是應該實現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; 
     } 
    }); 
0

你必須設置OnDragListener既要拖到View和您要下降到View