Note that there are some explanatory texts on larger screens.

plurals
  1. POSwipe Gesture inside ListView - Android
    text
    copied!<p>I am a newbie for Android. I got a tutorial on how to implement the <code>`listView</code> <a href="http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/" rel="nofollow">here</a>: </p> <p>I implemented it and it is working fine. I need to implement a <code>ViewSwitcher</code> for the same. Like when I swipe the song, I need to get some options like <em>play</em>, <em>add to queue</em> etc.,</p> <p>Pls go through that tutorial and pls help. I'm struggling almost from 3 weeks.</p> <p>Hope someone will help me.</p> <p><strong>Edit 1</strong>: The below code is the main Activity:</p> <pre><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&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); 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 &lt;song&gt; for (int i = 0; i &lt; nl.getLength(); i++) { // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); Element e = (Element) nl.item(i); // adding each child node to HashMap key =&gt; 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&lt;?&gt; 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 &gt; 100 và khoảng cách từ điểm kéo đầu đến điểm kéo cuối &gt; 100 if( (xvelocity &gt; SWIPE_MIN_VELOCITY) &amp;&amp; (xdistance &gt; SWIPE_MIN_DISTANCE) ) { if(ev1X &gt; 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(); } </code></pre> <p>Below code the XML file.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/list_row_main" /&gt; &lt;include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/list_row_swipe" /&gt; &lt;/ViewSwitcher&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload