2014-09-26 2 views
0

두 개의 목록보기가 있습니다. 그리고 하나의 목록보기에서 항목을 끌어서 다른 목록보기의 특정 위치에 놓기를 원합니다. 어떻게 할 수 있습니까? 이것은 내 레이아웃입니다.ListView 끌어서 놓기

<LinearLayout 
       android:id="@+id/parentLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal" 
       android:weightSum="3" > 

       <LinearLayout 
        android:id="@+id/vert1" 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@drawable/bg_shadowed_frame" 
        android:gravity="center_horizontal" 
        android:orientation="vertical" 
        android:padding="15dip" > 

        <TextView 
         android:id="@+id/tv1" 
         style="@style/TextViewMedStyleShadowWhite" 
         android:layout_width="wrap_content" 
         android:layout_height="30dip" 
         android:text="LIST 1" 
         android:textColor="@color/theme_blue" /> 

        <View 
         android:layout_width="match_parent" 
         android:layout_height="1dip" 
         android:layout_marginLeft="20dip" 
         android:layout_marginRight="20dip" 
         android:layout_marginTop="5dip" 
         android:background="@color/theme_blue" /> 

        <RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 

         <ListView 
          android:id="@+id/listView1" 
          android:layout_width="wrap_content" 
          android:layout_height="match_parent" 
          android:divider="@null" 
          android:paddingLeft="10dip" 
          android:paddingRight="10dip" /> 

         <ProgressBar 
          android:id="@+id/progres1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerInParent="true" 
          android:indeterminate="true" /> 
        </RelativeLayout> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/vert2" 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@drawable/bg_shadowed_frame" 
        android:gravity="center_horizontal" 
        android:orientation="vertical" 
        android:padding="15dip" > 

        <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal" 
         android:paddingLeft="15dip" 
         android:paddingRight="15dip" > 

         <TextView 
          android:id="@+id/tv1" 
          style="@style/TextViewMedStyleShadowWhite" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:layout_centerVertical="true" 
          android:text="LIST 2" 
          android:textColor="@color/theme_blue" /> 

         <ImageButton 
          android:id="@+id/addButton" 
          android:layout_width="30dip" 
          android:layout_height="30dip" 
          android:layout_alignParentRight="true" 
          android:background="@null" 
          android:scaleType="fitCenter" 
          android:src="@drawable/ic_mini_add" /> 
        </RelativeLayout> 

        <View 
         android:layout_width="match_parent" 
         android:layout_height="1dip" 
         android:layout_marginLeft="20dip" 
         android:layout_marginRight="20dip" 
         android:layout_marginTop="5dip" 
         android:background="@color/theme_blue" /> 

        <RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 

         <ListView 
          android:id="@+id/listView2" 
          android:layout_width="wrap_content" 
          android:layout_height="match_parent" 
          android:divider="@null" 
          android:paddingLeft="10dip" 
          android:paddingRight="10dip" /> 

         <ProgressBar 
          android:id="@+id/progres2" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerInParent="true" 
          android:indeterminate="true" /> 
        </RelativeLayout> 
       </LinearLayout> 
     </LinearLayout> 

여기에는 두 개의 ListView가 있습니다. 지금 내 긴 클릭 리스너에 내가 항목이

listView2.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       View selectedView = listView2.getChildAt(arg2); 
       selectedView.setVisibility(View.INVISIBLE); 
       mCellIsMobile = true; 

       return false; 
      } 
     }); 


listView2.setOnTouchListener(new View.OnTouchListener() { 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 

        int xPos = (int)event.getX(); 
        int yPos = (int)event.getY(); 
        Log.i("Touched","ListView2"+xPos+" , "+yPos); 

        parentLayout.requestDisallowInterceptTouchEvent(false); 
        parentLayout.onInterceptTouchEvent(event); 

        return false; 
       } 
      }); 

또한 parentLayout는 또한 터치 리스너를 가지고 있지만 아이가 그것을 소비로 터치 부모를 결코 도달하지를 클릭 숨 깁니다. You want to drop item at a SPECIFIC place on second listview 코드 아래에만 두 번째 목록의 END 또는 중 하나를 목록의 시작을 추가 :

parentLayout.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

       int xPos = (int)event.getX(); 
       int yPos = (int)event.getY(); 
       Log.i("Touched","Parent"+xPos+" , "+yPos); 
    return false; 
      } 
     }); 

답변

1

아래 코드의 유일한 문제는있다.

Basically the problem is I can't find the exact position where you will be dropping the item on 2nd ListView. 알 수있는 경우 POSITION_TO_DROP 변수를 해당 값으로 바꿉니다.

int POSITION_TO_DROP = 0; 
droppedList.add(POSITION_TO_DROP, droppedItem); 

하거나 2 목록보기의 끝 부분에 추가 할 때 사용은

droppedList.add(droppedItem); 

이러한 이미지는

Dragging

드롭 후

, 그것은 제 2의 시작에 추가합니다 참조 명부.

And After Droping

코드 :


//onCreate Start 
    targetLayout = (LinearLayout) findViewById(R.id.targetlayout); 
    listSource = (ListView) findViewById(R.id.sourcelist); 
    listTarget = (ListView) findViewById(R.id.targetlist); 
    comments = (TextView) findViewById(R.id.comments); 

    // Create and set the tags for the Buttons 

    final String SOURCELIST_TAG = "listSource"; 
    final String TARGETLIST_TAG = "listTarget"; 
    final String TARGETLAYOUT_TAG = "targetLayout"; 

    listSource.setTag(SOURCELIST_TAG); 
    listTarget.setTag(TARGETLIST_TAG); 
    targetLayout.setTag(TARGETLAYOUT_TAG); 

    listSource.setAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, month)); 
    listSource.setOnItemLongClickListener(listSourceItemLongClickListener); 

    droppedList = new ArrayList<String>(); 
    droppedAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, droppedList); 
    listTarget.setAdapter(droppedAdapter); 

    listSource.setOnDragListener(myDragEventListener); 
    targetLayout.setOnDragListener(myDragEventListener); 

    // OnCreate End 


    OnItemLongClickListener listSourceItemLongClickListener = new OnItemLongClickListener() { 

    @Override 
    public boolean onItemLongClick(AdapterView<?> l, View v, int position, 
      long id) { 

     // Selected item is passed as item in dragData 
     ClipData.Item item = new ClipData.Item(month[position]); 

     String[] clipDescription = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
     ClipData dragData = new ClipData((CharSequence) v.getTag(), 
       clipDescription, item); 
     DragShadowBuilder myShadow = new MyDragShadowBuilder(v); 

     v.startDrag(dragData, // ClipData 
       myShadow, // View.DragShadowBuilder 
       month[position], // Object myLocalState 
       0); // flags 

     commentMsg = v.getTag() + " : onLongClick.\n"; 
     comments.setText(commentMsg); 

     return true; 
    } 
}; 

private static class MyDragShadowBuilder extends View.DragShadowBuilder { 
    private static Drawable shadow; 

    public MyDragShadowBuilder(View v) { 
     super(v); 
     shadow = new ColorDrawable(Color.LTGRAY); 
    } 

    @Override 
    public void onProvideShadowMetrics(Point size, Point touch) { 
     int width = getView().getWidth(); 
     int height = getView().getHeight(); 

     shadow.setBounds(0, 0, width, height); 
     size.set(width, height); 
     touch.set(width/2, height/2); 
    } 

    @Override 
    public void onDrawShadow(Canvas canvas) { 
     shadow.draw(canvas); 
    } 

} 

protected class MyDragEventListener implements View.OnDragListener { 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     final int action = event.getAction(); 

     switch (action) { 
     case DragEvent.ACTION_DRAG_STARTED: 
      // All involved view accept ACTION_DRAG_STARTED for 
      // MIMETYPE_TEXT_PLAIN 
      if (event.getClipDescription().hasMimeType(
        ClipDescription.MIMETYPE_TEXT_PLAIN)) { 
       commentMsg += v.getTag() 
         + " : ACTION_DRAG_STARTED accepted.\n"; 
       comments.setText(commentMsg); 
       return true; // Accept 
      } else { 
       commentMsg += v.getTag() 
         + " : ACTION_DRAG_STARTED rejected.\n"; 
       comments.setText(commentMsg); 
       return false; // reject 
      } 
     case DragEvent.ACTION_DRAG_ENTERED: 
      commentMsg += v.getTag() + " : ACTION_DRAG_ENTERED.\n"; 
      comments.setText(commentMsg); 
      return true; 
     case DragEvent.ACTION_DRAG_LOCATION: 
      commentMsg += v.getTag() + " : ACTION_DRAG_LOCATION - " 
        + event.getX() + " : " + event.getY() + "\n"; 
      comments.setText(commentMsg); 
      return true; 
     case DragEvent.ACTION_DRAG_EXITED: 
      commentMsg += v.getTag() + " : ACTION_DRAG_EXITED.\n"; 
      comments.setText(commentMsg); 
      return true; 
     case DragEvent.ACTION_DROP: 
      // Gets the item containing the dragged data 
      ClipData.Item item = event.getClipData().getItemAt(0); 

      commentMsg += v.getTag() + " : ACTION_DROP" + "\n"; 
      comments.setText(commentMsg); 

      // If apply only if drop on buttonTarget 
      if (v == targetLayout) { 
       String droppedItem = item.getText().toString(); 

       commentMsg += "Dropped item - " + droppedItem + "\n"; 
       comments.setText(commentMsg); 

       int POSITION_TO_DROP = 0; 
       droppedList.add(POSITION_TO_DROP, droppedItem); 

       droppedAdapter.notifyDataSetChanged(); 

       return true; 
      } else { 
       return false; 
      } 

     case DragEvent.ACTION_DRAG_ENDED: 
      if (event.getResult()) { 
       commentMsg += v.getTag() 
         + " : ACTION_DRAG_ENDED - success.\n"; 
       comments.setText(commentMsg); 
      } else { 
       commentMsg += v.getTag() + " : ACTION_DRAG_ENDED - fail.\n"; 
       comments.setText(commentMsg); 
      } 
      ; 
      return true; 
     default: // unknown case 
      commentMsg += v.getTag() + " : UNKNOWN !!!\n"; 
      comments.setText(commentMsg); 
      return false; 

     } 
    } 
} 

여기 => Drag and Drop Between 2 ListViews

+0

덕분에 전체 프로젝트를 확인 적어도이 시작하는 것이 좋다. 나중에 특정 위치에 떨어지도록 구현할 수 있습니다. –