1

我是安卓新手。我有一个关于如何实现`listView 这里的教程:

我实现了它,它工作正常。我需要实现一个ViewSwitcher相同的。就像我滑动歌曲时一样,我需要一些选项,比如播放添加到队列等,

请仔细阅读该教程并提供帮助。我几乎从 3 周起就在苦苦挣扎。

希望有人能帮助我。

编辑1:以下代码是主要活动:

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();
}

下面对 XML 文件进行编码。

<?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>
4

2 回答 2

6

据我了解您的问题,您的问题是检测 ListView 中的触摸手势,对吗?

因此,您必须扩展 ListView 并在onInterceptTouchEvent方法中检测水平触摸手势。

下面是一个 ListView 的示例,它不会对水平触摸事件做出反应。这些事件将被分派给您的子视图,这实际上是您的列表项视图,它必须捕获这些事件(使用手势侦听器)并触发 ViewSwitcher。

public class SampleListView extends ListView {

.
.
.

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // reset difference values
                mDiffX = 0;
                mDiffY = 0;

                mLastX = ev.getX();
                mLastY = ev.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                mDiffX += Math.abs(curX - mLastX);
                mDiffY += Math.abs(curY - mLastY);
                mLastX = curX;
                mLastY = curY;

                // don't intercept event, when user tries to scroll vertically
                if (mDiffX > mDiffY) {
                    return false; // do not react to horizontal touch events, these events will be passed to your list item view
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

.
.
.

}
于 2012-07-25T10:08:14.550 回答
0

你可以用motionevent它来做。你只需要追踪速度。

例如。

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
       try {
           if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
               return false;
           // right to left swipe
           if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {                  
              Toast.makeText(getApplicationContext(), "Left Swipe", 
              Toast.LENGTH_SHORT).show();                  
            } 
于 2012-07-25T10:10:46.780 回答