skip to main | skip to sidebar

Android Development Tutorial

Pages

  • Home
 
  • RSS
  • Twitter
Related Posts Plugin for WordPress, Blogger...
Friday, October 26, 2012

Reordering of listview

Posted by Raju Gupta at 3:46 AM – 0 comments
 

Listview rows order can be changed using drag and drop functionality.Dragging should be enabled beyond the visible listview position.



<?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="wrap_content">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon" android:id="@+id/entryIcon"></ImageView>
<TextView android:layout_height="wrap_content" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_centerVertical="true" android:text="Demo description" android:layout_toRightOf="@+id/entryIcon"></TextView>
</RelativeLayout>




<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/mainAbsoluteLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">


<com.myself.dragndrop.CustomListView
 android:id="@+id/android:list"
 android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
</com.myself.dragndrop.CustomListView>

</AbsoluteLayout>

//CustomListView.java:

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.AbsListView.LayoutParams;

public class CustomListView extends ListView {
 
 public View moveView = null;
 private Context context = null;
 public int movedPosition = 0;
 public Entry moveEntry = null;
 
 public boolean isMoveFlag = false;
 private int firstPosition;
 private int lasPosition;
 private ArrayList<com.myself.dragndrop.CustomListView.Coordinates> listCoordinates;
 
 public CustomListView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  this.context = context;
  setVerticalScrollBarEnabled(false);

 }

 public CustomListView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  this.context = context;
  setVerticalScrollBarEnabled(true);

 }
 
 

 
 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  // TODO Auto-generated method stub
  if (isMoveFlag) {
   switch (ev.getAction()) {
   case MotionEvent.ACTION_DOWN:
    Log.v(getClass().toString(),"action_dowm");
    int y = (int)ev.getY();
    if (getParent() instanceof AbsoluteLayout) {
     AbsoluteLayout parent = (AbsoluteLayout)getParent();
     if (moveView != null) {
      parent.removeView(DnDAdapter.emptyView);
      parent.addView(DnDAdapter.emptyView, new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0, y - 22));
      
     }
    }
    break;
   case MotionEvent.ACTION_MOVE:
    Log.v(getClass().toString(),"action_move");
    int yMove = (int)ev.getY();
    if (getParent() instanceof AbsoluteLayout) {
     AbsoluteLayout parent = (AbsoluteLayout)getParent();
     if (moveView != null) {
      parent.removeView(DnDAdapter.emptyView);
      parent.addView(DnDAdapter.emptyView, new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0, yMove - 22));
      inChild(MotionEvent.ACTION_MOVE);
     }
    }
    break;
   case MotionEvent.ACTION_UP: 
    if (getParent() instanceof AbsoluteLayout) {
     AbsoluteLayout parent = (AbsoluteLayout)getParent();
     if (moveView != null) {
      parent.removeView(DnDAdapter.emptyView);
      inChild(MotionEvent.ACTION_UP);
//      DnDAdapter.addEntry(movedPosition);
     }
    }
    isMoveFlag = false;
    setEnabled(true);
    Log.v(getClass().toString(),"action_up: " + isMoveFlag);
    break;
   default:
    break;
  }
  }
  return super.onTouchEvent(ev);
 }
 
 @Override
 public void setOnScrollListener(OnScrollListener l) {
  // TODO Auto-generated method stub
  l = new OnScrollListener() {
   
   @Override
   public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub
     firstPosition = getFirstVisiblePosition();
     lasPosition = getLastVisiblePosition();
   }
   
   @Override
   public void onScroll(AbsListView view, int firstVisibleItem,
     int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub
    
   }
  };
  super.setOnScrollListener(l);
 }
 
 private void inChild(int action) {
  setCoordinates();
  for (int i = 0; i < listCoordinates.size(); i++) {
   System.err.println("size"+listCoordinates.size());
   Coordinates coordinates = listCoordinates.get(i);
   int topPosition = coordinates.getTop();
   System.err.println("coordinates.getTop()"+coordinates.getTop());
   System.err.println("coordinates.getBottom()"+coordinates.getBottom());
   int bottomPosition = coordinates.getBottom();
   int bottomMoveViewPosition = DnDAdapter.emptyView.getBottom();
   if (i != 0) {
    if (bottomMoveViewPosition > topPosition && bottomMoveViewPosition < bottomPosition) {
     Log.v(getClass().toString(),"Move view in position: " + i);
     DnDAdapter.removeEmptyEntry();
     DnDAdapter.addEmptyEntry(getFirstVisiblePosition() + i);
     if (action == MotionEvent.ACTION_UP) {
      DnDAdapter.removeEmptyEntry();
      DnDAdapter.addEntry(getFirstVisiblePosition() + i);
     }
    }
   }
  }
 }
 
 private void setCoordinates() {
  listCoordinates = new ArrayList<Coordinates>();
  int count = getChildCount();
  System.err.println("count"+count);
  for (int i = 0; i < count; i++) {
   View child = getChildAt(i);
   if (child != null) {
    Coordinates coord = new Coordinates();
    coord.setTop(child.getTop());
    coord.setBottom(child.getBottom());
    listCoordinates.add(coord);
   }
  }
 }
 
 
 
 class Coordinates {
  
  private int top = 0;
  private int bottom = 0;
  
  public int getTop() {
   return top;
  }
  public void setTop(int top) {
   this.top = top;
  }
  public int getBottom() {
   return bottom;
  }
  public void setBottom(int bottom) {
   this.bottom = bottom;
  }
  
  
 }

}


//ReorderAdapter.java:

import java.util.ArrayList;
import java.util.List;

import com.myself.dragndrop.R;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ReorderAdapter extends BaseAdapter {
 
 private static List<Entry> items = null;
 private Context context = null;
 
 public static View emptyView = null;
 
 private static Entry emptyEntry = new Entry();
 
 public ReorderAdapter(Context context) {
  // TODO Auto-generated constructor stub
  this.context = context;
//  buildList();
 }
 
 
 
 public List<Entry> getItems() {
  return items;
 }
 
 public void setList(List<Entry> items) {
  this.items = items;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  System.err.println("items.size()"+items.size());
  return items.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return items.get(position);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return 0;
 }

 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  Entry entry = items.get(position);
  System.err.println("getview"+position);
  
  LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  final View view = inflater.inflate(R.layout.listitem, parent, false);
  TextView txt = (TextView)view.findViewById(R.id.TextView01);
  txt.setText(entry.getName());
  txt.setTextColor(Color.WHITE);
  final ImageView img = (ImageView)view.findViewById(R.id.entryIcon);
  img.setOnTouchListener(new OnTouchListener() {
   
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    Log.v(getClass().toString(),"Icon onTouch. Selected position: " + position);
    
    DragAndDrop.listView.moveEntry = items.get(position);
    buildEmptyView(DragAndDrop.listView.moveEntry);
    items.remove(position);
    items.add(position, emptyEntry);
    DragAndDrop.listView.invalidateViews();
    DragAndDrop.listView.setEnabled(false);
    DragAndDrop.listView.isMoveFlag = true;
    DragAndDrop.listView.moveView = view;
    DragAndDrop.listView.movedPosition = position;
    return false;
   }
  });
  if (entry.getName().equals("Empty entry")) {
   img.setVisibility(View.INVISIBLE);
   txt.setVisibility(View.INVISIBLE);
  }
  return view;
 }

 private void buildEmptyView(Entry moveentry) {
  LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  emptyView = inflater.inflate(R.layout.emptyview, null, false);
  TextView txt = (TextView)emptyView.findViewById(R.id.TextView01);
  txt.setText(moveentry.getName());
  txt.setTextColor(Color.WHITE);
 }
 
 public static void addEntry(int pos) {
  items.add(pos, DragAndDrop.listView.moveEntry);
  DragAndDrop.listView.invalidateViews();
 }
 
 public static void addEmptyEntry(int pos) {
  items.add(pos, emptyEntry);
  DragAndDrop.listView.invalidateViews();
 }
 
 public static void removeEmptyEntry() {
  items.remove(emptyEntry);
  DragAndDrop.listView.invalidateViews();
 }
}

//DragAndDrop.java :

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class DragAndDrop extends ListActivity {
    /** Called when the activity is first created. */
 
 
 public static LinearLayout layout = null;
 public static ScrollView scrollView = null;
 public static CustomListView listView = null; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ReorderAdapter adapter = new ReorderAdapter(this);
        adapter.setList(buildList());
        listView = (CustomListView)getListView();
        listView.setAdapter(adapter);
        
    }
    
    private List<Entry> buildList() {
  List<Entry> items = new ArrayList<Entry>();
  for (int i = 0; i < 30; i++) {
   Entry entry = new Entry();
   entry.setName("Demo entry " + i);
   items.add(entry);
  }
  return items;
 }
    
    
}
Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Older Post
Subscribe to: Post Comments (Atom)

More Technical Blogs

  • Tech Savvy - Technology Tips
  • PHP Video Tutorial
  • Java Programs with Output
  • C Programming Tutorial
  • Linux Tutorial
  • Language Tutorial
  • Web Development tutorial
  • Popular
  • Recent
  • Archives

Popular Posts

  • Get XML from APK
    Step 1. Pass the <APK> file path+name in getIntents(...) method. Step 2. Run the attached code. This will convert all layo...
  • Android SMS Reader
    SMS Reader is an application that read all incoming SMS loudly.And it has feature of reading existing SMS using navigation keys. package...
  • Android SeekBar
    Whenever the progress bar of SeekBar is moved, it will call the inbuild method of OnProgressChange(), inside which we can write our functio...
  • Android Stub uninstalling the existing client and launching another client
    This Android Application uninstall prior version of client from the device. It checks for the newer version of client on the device and l...
  • Android Architecture
    Overview of Android Platform Before getting into developing android applications it is better to take a few minutes to know what you a...
  • Android Video Tutorial: Android Application Development - Traceview
    In this 40-minute tutorial, Marko will show you how you can use the Android SDK tool Traceview to analyze data and debug issues in your An...
  • Android Video Tutorial - What is Android?
    This is the first segment from a 5 week online course, Developing Android Applications with Java. In this segment, instructor Tony Hillers...
  • How to Program for Android: Video Tutorial 8 - Add and Read Text File
    How to add and read from a text file on an android device. Part 1: Part 2:
  • How To Program for Android: Video Tutorial 6 - Drop Down Menu
    How to use the drop down menu, or spinner widget in Android. Part 1: Part 2 :
  • Android Video Tutorial - build the Simple Flashlight App
    This is the second segment from a 5 week online course, Developing Android Applications with Java. In this segment, instructor Tony Hiller...
Powered by Blogger.

Archives

  • ▼  2012 (44)
    • ▼  October (31)
      • Reordering of listview
      • Text-to-Speech capability for Android Devices
      • Android Stub uninstalling the existing client and ...
      • Tab creation in android screen
      • ListView Recordering by drag drop in Android
      • Android app for SimpleWiktionary
      • App in android for random quote generation
      • Implementing ListView-Adapter in Android
      • Extract all java classes from APK
      • Get XML from APK
      • Sending SMS from the Android Application
      • How to create Drop Down in Android
      • Android Page Navigation
      • To retrieve the cell info for Android device
      • Creating the enrty in the agenda for Android devices.
      • Test Application for Samsung Android devices
      • Color Picker from Image in Android
      • Image Switcher & Gallery in Android
      • Andorid application that listens to incoming sms
      • Accelerometer management for the Android devices
      • Alert box for Confirm
      • 3D Rotation in Android
      • Custom Bar Control for Android using Java
      • Creating Layouts TableRows and TextViews dynamical...
      • Android SeekBar
      • Dialing phone number from Google Android Application
      • Login Screen Creation using Android
      • Add a Progress Bar to Android based Mobile Screens
      • Adding a button to Android based mobile screens
      • Android Dynamically generating views
      • Timezone converter
    • ►  September (3)
    • ►  March (1)
    • ►  February (9)
  • ►  2011 (69)
    • ►  December (69)
 

Followers

Labels

  • Activities (9)
  • Andoird Menu (2)
  • Android timelineActivity. (1)
  • Android Adapter (1)
  • Android app (9)
  • Android App Inventor (1)
  • Android App Publishing (2)
  • Android Application Components (3)
  • Android Application Fundamental (2)
  • Android Architecture (1)
  • Android AsyncTask (1)
  • Android Basic (7)
  • Android Bootcamp Training (18)
  • Android Button Widget (3)
  • Android Custom Style (1)
  • Android Dialog (1)
  • Android Drawable (2)
  • Android Environment (1)
  • Android example (9)
  • Android File System (2)
  • Android Geolocation (2)
  • Android ImageView (1)
  • Android Installation (8)
  • Android intents (5)
  • Android lifecycle (1)
  • Android LIst (4)
  • Android Listener (4)
  • Android Manifest (3)
  • Android Market (1)
  • Android Notification (1)
  • Android Object (1)
  • Android Package File (1)
  • Android Platform (1)
  • Android service (4)
  • Android StatusActivity (1)
  • Android Theme Style (3)
  • Android Traceview (1)
  • Android UI (6)
  • Android Unit Testing (1)
  • Android Widget (4)
  • AndroidManifest.xml (4)
  • Application Icon (1)
  • Broadcast Receiver (2)
  • Content Providers (1)
  • Creating Activities (1)
  • Creating Custom Styles in Android (1)
  • Creating Multiple Activities (1)
  • Database (3)
  • draw9patch (1)
  • Eclipse (12)
  • Explicit Intents (2)
  • Explicit Intents Example (1)
  • Hello world with Android (1)
  • Helloworld with Android (5)
  • Implicit Intents (2)
  • Implicit Intents Example (1)
  • Layout View (3)
  • lifemichael (8)
  • Location Sensor (1)
  • Multiple Activities (2)
  • Netbeans (1)
  • OpenGL ES Graphics (1)
  • Passing Values with Intents (2)
  • Project Structure (1)
  • Retrieving Image URI from Intents (1)
  • Setting Android Environment (1)
  • SQLite (3)
  • TGENT (8)
  • UserGroupAtGoogle (8)
  • XML (1)
  • xtensive arts Training (11)
 
 
© 2011 Android Development Tutorial | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger