Drag and drop listview android:
The main use of the Drag and drop listview android is you can re-arrange the listview by drag and drop in listview items.
Readmore :Drag and drop listview android.
Steps to follow inDrag and drop listview android:
Step 1:
Create new project dropdownlistview.
Import Drag and drop listview android in your project.
Now,Create new class TouchListViewDemo,
TouchListViewDemo.Java
publicclass TouchListViewDemo extends ListActivity {
String[] head_list={"A","B","C","D","E","F","G","H","I"};
private IconicAdapter adapter=null;
private ArrayList<String> array=new ArrayList<String>(Arrays.asList(head_list));
@Override
publicvoid onCreate(Bundle icicle) {
super.onCreate(icicle);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
TouchListView tlv=(TouchListView)getListView();
adapter=new IconicAdapter();
setListAdapter(adapter);
tlv.setDropListener(onDrop);
tlv.setOnItemClickListener(new OnItemClickListener()
{
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
}
});
}
private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
@Override
publicvoid drop(int from, int to) {
String item=adapter.getItem(from);
adapter.remove(item);
adapter.insert(item, to);
}
};
class IconicAdapter extends ArrayAdapter<String> {
IconicAdapter() {
super(TouchListViewDemo.this, R.layout.row2, array);
}
public View getView(int position, View convertView,
ViewGroup parent) {
View row=convertView;
if (row==null) {
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row2, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(array.get(position));
return(row);
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.commonsware.cwac.tlv.TouchListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tlv="http://schemas.android.com/apk/res/com.vel.dropanddownlistview"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
tlv:normal_height="64dip"
tlv:expanded_height="128dip"
tlv:grabber="@+id/icon"
tlv:remove_mode="slideRight"
/>
Then Create row.xml for set adapter for the listview,
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dip"
android:gravity="center_vertical"
android:ignoreGravity="@+id/icon">
<ImageView android:id="@+id/icon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/grabber"
/>
<TextView android:id="@+id/label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:paddingLeft="9dip"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:layout_centerVertical="true"
android:ellipsize="marquee"
android:singleLine="true"
/>
</RelativeLayout>
Output of the Drag and drop listview android is:
Here B moved to the top in the listview.
No comments:
Post a Comment