Improved Drag Too Drib Items Betwixt Listview

Posted on

Last postal service demo my outset essay “Drag in addition to Drop items betwixt ListView“. The source in addition to destination of the Drag in addition to Drop functioning is difficult coded in addition to fixed. It’s also un-flexible! It is improved hither to supply more flexible approach.


In guild to instruct the associated ListView from whatever LinearLayout to endure drop, I create a novel degree LinearLayoutListView extends LinearLayout, associated alongside a ListView.

LinearLayoutListView.java

package com.example.androidimageviewlist;  import android.content.Context; import android.util.AttributeSet; import android.widget.LinearLayout; import android.widget.ListView;  world degree LinearLayoutListView extends LinearLayout {    ListView listView;   world LinearLayoutListView(Context context) {   super(context);   // TODO Auto-generated constructor stub  }   world LinearLayoutListView(Context context, AttributeSet attrs) {   super(context, attrs);   // TODO Auto-generated constructor stub  }   world LinearLayoutListView(Context context, AttributeSet attrs,    int defStyleAttr) {   super(context, attrs, defStyleAttr);   // TODO Auto-generated constructor stub  }   world void setListView(ListView lv){   listView = lv;  }  } 

Modify /res/layout/activity_main.xml, to role <com.example.androidimageviewlist.LinearLayoutListView>. And also display 3 to demo the flexibility of the novel approach.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="4dp"     tools:context="com.example.androidimageviewlist.MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <LinearLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="2"         android:background="@android:color/background_dark"         android:orientation="horizontal" >          <com.example.androidimageviewlist.LinearLayoutListView             android:id="@+id/pane1"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_margin="4dp"             android:layout_weight="1"             android:background="@android:color/background_light"             android:orientation="vertical" >              <ListView                 android:id="@+id/listview1"                 android:layout_width="match_parent"                 android:layout_height="wrap_content" />         </com.example.androidimageviewlist.LinearLayoutListView>          <com.example.androidimageviewlist.LinearLayoutListView             android:id="@+id/pane2"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_margin="4dp"             android:layout_weight="1"             android:background="@android:color/background_light"             android:orientation="vertical" >              <ListView                 android:id="@+id/listview2"                 android:layout_width="match_parent"                 android:layout_height="wrap_content" />         </com.example.androidimageviewlist.LinearLayoutListView>                  <com.example.androidimageviewlist.LinearLayoutListView             android:id="@+id/pane3"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_margin="4dp"             android:layout_weight="1"             android:background="@android:color/background_light"             android:orientation="vertical" >              <ListView                 android:id="@+id/listview3"                 android:layout_width="match_parent"                 android:layout_height="wrap_content" />         </com.example.androidimageviewlist.LinearLayoutListView>     </LinearLayout>      <LinearLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1"         android:orientation="horizontal" >          <TextView             android:id="@+id/prompt"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:background="@android:color/black"             android:gravity="bottom"             android:textColor="@android:color/white" />     </LinearLayout>  </LinearLayout> 

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 degree MainActivity extends ActionBarActivity {   //items stored inwards ListView  world degree Item {   Drawable ItemDrawable;   String ItemString;   Item(Drawable drawable, String t){    ItemDrawable = drawable;    ItemString = t;   }  }    //objects passed inwards Drag in addition to Drop functioning  degree PassObject{   View view;   Item item;   List<Item> srcList;      PassObject(View v, Item i, List<Item> s){    stance = v;    exceptional = i;    srcList = s;   }  }    static degree ViewHolder {   ImageView icon;   TextView text;   }   world degree ItemsListAdapter extends BaseAdapter {      someone Context context;   someone List<Item> list;    ItemsListAdapter(Context c, List<Item> l){    context = c;    listing = l;   }    @Override   world int getCount() {    render list.size();   }    @Override   world Object getItem(int position) {    render list.get(position);   }    @Override   world long getItemId(int position) {    render 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);        render rowView;   }      world List<Item> getList(){    render list;   }  }   List<Item> items1, items2, items3;  ListView listView1, listView2, listView3;  ItemsListAdapter myItemsListAdapter1, myItemsListAdapter2, myItemsListAdapter3;  LinearLayoutListView area1, area2, area3;  TextView prompt;   @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 destination 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("");     render true;     }});   }    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);        render 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()) {     instance DragEvent.ACTION_DRAG_STARTED:      prompt.append("ACTION_DRAG_STARTED: " + expanse  + "\n");      break;      instance DragEvent.ACTION_DRAG_ENTERED:      prompt.append("ACTION_DRAG_ENTERED: " + expanse  + "\n");      break;      instance DragEvent.ACTION_DRAG_EXITED:      prompt.append("ACTION_DRAG_EXITED: " + expanse  + "\n");      break;      instance 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();            break;       instance DragEvent.ACTION_DRAG_ENDED:        prompt.append("ACTION_DRAG_ENDED: " + expanse  + "\n");          default:        break;        }           render 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();   }     };   someone 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 exceptional = novel Item(d, s);    items1.add(item);   }      arrayDrawable.recycle();   arrayText.recycle();  }    someone boolean removeItemToList(List<Item> l, Item it){   boolean number = l.remove(it);   render result;  }    someone boolean addItemToList(List<Item> l, Item it){   boolean number = l.add(it);   render result;  }  } 

/res/layout/row.xml in addition to /res/values/arrays.xml, get upward to previous postal service of “Custom ListView alongside ImageView“.

To role Drag in addition to Drop on your app, you lot accept to alter AndroidManifest.xml to laid android:minSdkVersion=”11″.

 The source in addition to destination of the Drag in addition to Drop functioning is difficult coded in addition to fixed Improved Drag in addition to Drop items betwixt ListViewDownload the files.

In this exercise, it drag a ListView item, driblet on a LinearLayout, thence add together the exceptional to the associated ListView, ever on bottom. Not driblet on ListView actually.

In this exercise, “Drag in addition to Drop items betwixt ListView, alongside OnDragListener on ListView cells“, about other OnDragListener (ItemOnDragListener) implemented for rowView of ListView items, such that nosotros tin genuinely driblet on ListView items, in addition to insert inwards the previous position.