Drag As Well As Drib Items Betwixt Listview, Amongst Ondraglistener On Listview Cells

Posted on

In concluding ii postal service “Drag too Drop items betwixt ListView” too “Improved Drag too Drop items betwixt ListView“, it drag a ListView item, drib on a LinearLayout, too thus add together the detail to the associated ListView, e’er on bottom. Not drib on ListView actually.

In this exercise, another OnDragListener (ItemOnDragListener) implemented for rowView of ListView items, such that nosotros tin truly drib on ListView items, too insert inward the previous position.


MainActivity.java

package com.example.androidimageviewlist;  import java.util.ArrayList; import java.util.List;  import android.support.v7.app.ActionBarActivity; import android.text.method.ScrollingMovementMethod; import android.app.Activity; import android.content.ClipData; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.DragEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.DragShadowBuilder; import android.view.View.OnDragListener; import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;  world shape MainActivity extends ActionBarActivity {   //items stored inward ListView  world shape Item {   Drawable ItemDrawable;   String ItemString;   Item(Drawable drawable, String t){    ItemDrawable = drawable;    ItemString = t;   }  }    //objects passed inward Drag too Drop performance  shape PassObject{   View view;   Item item;   List<Item> srcList;      PassObject(View v, Item i, List<Item> s){    stance = v;    detail = i;    srcList = s;   }  }    static shape ViewHolder {   ImageView icon;   TextView text;   }   world shape ItemsListAdapter extends BaseAdapter {      somebody Context context;   somebody List<Item> list;    ItemsListAdapter(Context c, List<Item> l){    context = c;    listing = l;   }    @Override   world int getCount() {    furnish list.size();   }    @Override   world Object getItem(int position) {    furnish list.get(position);   }    @Override   world long getItemId(int position) {    furnish position;   }    @Override   world View getView(int position, View convertView, ViewGroup parent) {    View rowView = convertView;           // reuse views       if (rowView == null) {        LayoutInflater inflater = ((Activity) context).getLayoutInflater();        rowView = inflater.inflate(R.layout.row, null);         ViewHolder viewHolder = novel ViewHolder();        viewHolder.icon = (ImageView) rowView.findViewById(R.id.rowImageView);        viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);        rowView.setTag(viewHolder);        }        ViewHolder holder = (ViewHolder) rowView.getTag();       holder.icon.setImageDrawable(list.get(position).ItemDrawable);       holder.text.setText(list.get(position).ItemString);              rowView.setOnDragListener(new ItemOnDragListener(list.get(position)));        furnish rowView;   }      world List<Item> getList(){    furnish list;   }  }   List<Item> items1, items2, items3;  ListView listView1, listView2, listView3;  ItemsListAdapter myItemsListAdapter1, myItemsListAdapter2, myItemsListAdapter3;  LinearLayoutListView area1, area2, area3;  TextView prompt;    //Used to resume master copy color inward drib ended/exited  int resumeColor;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   listView1 = (ListView)findViewById(R.id.listview1);   listView2 = (ListView)findViewById(R.id.listview2);   listView3 = (ListView)findViewById(R.id.listview3);      area1 = (LinearLayoutListView)findViewById(R.id.pane1);   area2 = (LinearLayoutListView)findViewById(R.id.pane2);   area3 = (LinearLayoutListView)findViewById(R.id.pane3);   area1.setOnDragListener(myOnDragListener);   area2.setOnDragListener(myOnDragListener);   area3.setOnDragListener(myOnDragListener);   area1.setListView(listView1);   area2.setListView(listView2);   area3.setListView(listView3);      initItems();   myItemsListAdapter1 = novel ItemsListAdapter(this, items1);   myItemsListAdapter2 = novel ItemsListAdapter(this, items2);   myItemsListAdapter3 = novel ItemsListAdapter(this, items3);   listView1.setAdapter(myItemsListAdapter1);   listView2.setAdapter(myItemsListAdapter2);   listView3.setAdapter(myItemsListAdapter3);      /*   //Auto scroll to terminate of ListView   listView1.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);   listView2.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);   listView3.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);   */      listView1.setOnItemClickListener(listOnItemClickListener);   listView2.setOnItemClickListener(listOnItemClickListener);   listView3.setOnItemClickListener(listOnItemClickListener);      listView1.setOnItemLongClickListener(myOnItemLongClickListener);   listView2.setOnItemLongClickListener(myOnItemLongClickListener);   listView3.setOnItemLongClickListener(myOnItemLongClickListener);      prompt = (TextView) findViewById(R.id.prompt);   // brand TextView scrollable   prompt.setMovementMethod(new ScrollingMovementMethod());   //clear prompt expanse if LongClick   prompt.setOnLongClickListener(new OnLongClickListener(){        @Override    world boolean onLongClick(View v) {     prompt.setText("");     furnish true;     }});      resumeColor  = getResources().getColor(android.R.color.background_light);   }    OnItemLongClickListener myOnItemLongClickListener = novel OnItemLongClickListener(){    @Override   world boolean onItemLongClick(AdapterView<?> parent, View view,     int position, long id) {    Item selectedItem = (Item)(parent.getItemAtPosition(position));        ItemsListAdapter associatedAdapter = (ItemsListAdapter)(parent.getAdapter());       List<Item> associatedList = associatedAdapter.getList();        PassObject passObj = novel PassObject(view, selectedItem, associatedList);     ClipData information = ClipData.newPlainText("", "");    DragShadowBuilder shadowBuilder = novel View.DragShadowBuilder(view);    view.startDrag(data, shadowBuilder, passObj, 0);        furnish true;   }     };    OnDragListener myOnDragListener = novel OnDragListener() {    @Override   world boolean onDrag(View v, DragEvent event) {    String area;    if(v == area1){     expanse = "area1";     }else if(v == area2){     expanse = "area2";     }else if(v == area3){     expanse = "area3";     }else{     expanse = "unknown";     }        switch (event.getAction()) {     illustration DragEvent.ACTION_DRAG_STARTED:      prompt.append("ACTION_DRAG_STARTED: " + expanse  + "\n");      break;      illustration DragEvent.ACTION_DRAG_ENTERED:      prompt.append("ACTION_DRAG_ENTERED: " + expanse  + "\n");      break;      illustration DragEvent.ACTION_DRAG_EXITED:      prompt.append("ACTION_DRAG_EXITED: " + expanse  + "\n");      break;      illustration DragEvent.ACTION_DROP:      prompt.append("ACTION_DROP: " + expanse  + "\n");       PassObject passObj = (PassObject)event.getLocalState();      View stance = passObj.view;      Item passedItem = passObj.item;      List<Item> srcList = passObj.srcList;      ListView oldParent = (ListView)view.getParent();      ItemsListAdapter srcAdapter = (ItemsListAdapter)(oldParent.getAdapter());            LinearLayoutListView newParent = (LinearLayoutListView)v;      ItemsListAdapter destAdapter = (ItemsListAdapter)(newParent.listView.getAdapter());         List<Item> destList = destAdapter.getList();            if(removeItemToList(srcList, passedItem)){       addItemToList(destList, passedItem);      }            srcAdapter.notifyDataSetChanged();      destAdapter.notifyDataSetChanged();            //smooth scroll to bottom      newParent.listView.smoothScrollToPosition(destAdapter.getCount()-1);            break;       illustration DragEvent.ACTION_DRAG_ENDED:        prompt.append("ACTION_DRAG_ENDED: " + expanse  + "\n");         default:        break;        }           furnish true;   }     };    shape ItemOnDragListener implements OnDragListener{      Item  me;      ItemOnDragListener(Item i){    me = i;   }    @Override   world boolean onDrag(View v, DragEvent event) {    switch (event.getAction()) {    illustration DragEvent.ACTION_DRAG_STARTED:     prompt.append("Item ACTION_DRAG_STARTED: " + "\n");     break;     illustration DragEvent.ACTION_DRAG_ENTERED:     prompt.append("Item ACTION_DRAG_ENTERED: " + "\n");     v.setBackgroundColor(0x30000000);     break;     illustration DragEvent.ACTION_DRAG_EXITED:     prompt.append("Item ACTION_DRAG_EXITED: " + "\n");     v.setBackgroundColor(resumeColor);     break;     illustration DragEvent.ACTION_DROP:     prompt.append("Item ACTION_DROP: " + "\n");      PassObject passObj = (PassObject)event.getLocalState();     View stance = passObj.view;     Item passedItem = passObj.item;     List<Item> srcList = passObj.srcList;     ListView oldParent = (ListView)view.getParent();     ItemsListAdapter srcAdapter = (ItemsListAdapter)(oldParent.getAdapter());          ListView newParent = (ListView)v.getParent();     ItemsListAdapter destAdapter = (ItemsListAdapter)(newParent.getAdapter());     List<Item> destList = destAdapter.getList();          int removeLocation = srcList.indexOf(passedItem);     int insertLocation = destList.indexOf(me);     /*      * If drag too drib on the same list, same position,      * ignore      */     if(srcList != destList || removeLocation != insertLocation){      if(removeItemToList(srcList, passedItem)){       destList.add(insertLocation, passedItem);      }            srcAdapter.notifyDataSetChanged();      destAdapter.notifyDataSetChanged();     }      v.setBackgroundColor(resumeColor);          break;      illustration DragEvent.ACTION_DRAG_ENDED:       prompt.append("Item ACTION_DRAG_ENDED: "  + "\n");       v.setBackgroundColor(resumeColor);      default:       break;       }         furnish true;   }     }    OnItemClickListener listOnItemClickListener = novel OnItemClickListener(){    @Override   world void onItemClick(AdapterView<?> parent, View view, int position,     long id) {    Toast.makeText(MainActivity.this,       ((Item)(parent.getItemAtPosition(position))).ItemString,       Toast.LENGTH_SHORT).show();   }     };   somebody void initItems(){   items1 = novel ArrayList<Item>();   items2 = novel ArrayList<Item>();   items3 = novel ArrayList<Item>();      TypedArray arrayDrawable = getResources().obtainTypedArray(R.array.resicon);   TypedArray arrayText = getResources().obtainTypedArray(R.array.restext);      for(int i=0; i<arrayDrawable.length(); i++){    Drawable d = arrayDrawable.getDrawable(i);    String sec = arrayText.getString(i);    Item detail = novel Item(d, s);    items1.add(item);   }      arrayDrawable.recycle();   arrayText.recycle();  }    somebody boolean removeItemToList(List<Item> l, Item it){   boolean number = l.remove(it);   furnish result;  }    somebody boolean addItemToList(List<Item> l, Item it){   boolean number = l.add(it);   furnish result;  }  } 

LinearLayoutListView.java too the layout file, /res/layout/activity_main.xml, cite to last post.

/res/layout/row.xml too /res/values/arrays.xml, cite to previous postal service of “Custom ListView amongst ImageView“.

To operate Drag too Drop on your app, you lot convey to alter AndroidManifest.xml to fix android:minSdkVersion=”11″.

Improved Drag too Drop items betwixt ListView Drag too Drop items betwixt ListView, amongst OnDragListener on ListView cellsDownload the files.

Next:
– Drag too Drop betwixt ListView too GridView