I am new to Android. I got a tutorial on how to implement `listView here :
I implemented it and it works fine. I need to implement ViewSwitcher for this. For example, when I scroll through a song, I need to get some parameters, such as play, queue, etc.
Pls goes through this tutorial and help with pls. I am afraid from almost 3 weeks.
Hope someone helps me.
Edit 1 : Below is the code:
import java.util.ArrayList; import java.util.HashMap; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.widget.ViewSwitcher; public class CustomizedListView extends Activity implements OnGestureListener{ // All static variables static final String URL = "http://api.androidhive.info/music/music.xml"; // XML node keys static final String KEY_SONG = "song"; // parent node static final String KEY_ID = "id"; static final String KEY_TITLE = "title"; static final String KEY_ARTIST = "artist"; static final String KEY_DURATION = "duration"; static final String KEY_THUMB_URL = "thumb_url"; private ViewSwitcher switcher1; private GestureDetector gesturedetector = null; ListView list; LazyAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML from URL Document doc = parser.getDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_SONG); // looping through all song nodes <song> for (int i = 0; i < nl.getLength(); i++) { // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); Element e = (Element) nl.item(i); // adding each child node to HashMap key => value map.put(KEY_ID, parser.getValue(e, KEY_ID)); map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST)); map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION)); map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); // adding HashList to ArrayList songsList.add(map); } list=(ListView)findViewById(R.id.list); // Getting adapter by passing xml data ArrayList adapter=new LazyAdapter(this, songsList); list.setAdapter(adapter); // Click event for single list row list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gesturedetector.onTouchEvent(event); } public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } int SWIPE_MIN_VELOCITY = 100; int SWIPE_MIN_DISTANCE = 100; //Sá»± kiện khi bạn vuá»'t mà n hình Ä'ưa sang má»™t bên nà o Ä'ó public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { //Get Position float ev1X = e1.getX(); float ev2X = e2.getX(); //Get distance of X (e1) to X (e2) final float xdistance = Math.abs(ev1X - ev2X); //Get veclocity of cusor //Váºn tá»'c = sá»' Ä'iểm ảnh (px) / giây final float xvelocity = Math.abs(velocityX); //Váºn tá»'c chuyển Ä'ổi X > 100 và khoảng cách từ Ä'iểm kéo Ä'ầu Ä'ến Ä'iểm kéo cuá»'i > 100 if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) ) { if(ev1X > ev2X)//Switch Left { previousView(); } else//Switch Right { nextView(); } } return false; } public void onLongPress(MotionEvent e) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { return false; } public void onShowPress(MotionEvent e) { } public boolean onSingleTapUp(MotionEvent e) { return false; } //Next, Previous Views private void previousView() { switcher1.showPrevious(); } private void nextView() { switcher1.showNext(); }
Below is the XML file code.
<?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/list_row_main" /> <include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/list_row_swipe" /> </ViewSwitcher>