手势列表视图的android视图、手势、列表、android

2023-09-12 22:08:15 作者:拎起奶瓶砸XX

您好,我有一个姿态加入到我的列表视图,我想要实现的联系人应用程序相同的功能。 当我离开刷卡,应该发送邮件时,右轻扫它应该调用。谁能帮助我如何做这些手势检测...我已经在其他各种意见实现了它......但我无法为ListView控件做...我不打算是什么?worng 我的code is`

  / **第一次创建活动时调用。 * /
公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    的setContentView(R.layout.main);
    //创建一个字符串数组,将被投入到我们的ListActivity
    的String []的名称=新的String [] {LINUX,Windows7的,日蚀,粟色,Ubuntu的中的Solaris,机器人,iPhone};
    //创建一个ArrayAdapter,这实际上使得弦乐以上
    //出现在ListView
    this.setListAdapter(新ArrayAdapter<字符串>(这一点,R.id.MyList,名称));
    gestureListener =新ListView.OnTouchListener(){
        公共布尔onTouch(视图V,MotionEvent事件){
            如果(gestureDetector.onTouchEvent(事件)){
                返回true;
            }
            返回false;
        }
    };
}

@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);
    //获取被点击的项目
    对象o = this.getListAdapter()的getItem(位置)。
    字符串关键字= o.toString();
    Toast.makeText(这一点,你已经选择:+关键字,Toast.LENGTH_LONG).show();
}

@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
    如果(gestureDetector.onTouchEvent(事件))
        返回true;
    其他
        返回false;
}

类MyGestureDetector扩展SimpleOnGestureListener {

    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
        尝试 {
            如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH){
                返回false;
                //从右向左轻扫
            }
            如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                上下文CTX = getApplicationContext();
                CharSequence的TXT =从右至左刷卡;
                INT持续时间= Toast.LENGTH_LONG;
                吐司面包= Toast.makeText(CTX,TXT,持续时间);
                toast.show();
                Toast.makeText(this.getItem(lv.pointToPosition((int)的e1.getX(),(int)的e1.getY())));
                //返回super.onFling();
            }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                上下文CTX = getApplicationContext();
                CharSequence的TXT =从左向右滑动
                INT持续时间= Toast.LENGTH_LONG;
                吐司面包= Toast.makeText(CTX,TXT,持续时间);
                toast.show();
            }
        }赶上(例外五){
            //什么
        }
        返回false;
    }
}
 

解决方案

根据从后[#937313]答案[1](要感谢GAV和paiego),我掀起了以下code认识简单手势的ListView(水平刷卡)。

不过,一扔操作之后,ListView的 onItemClick()监听器将被调用呢!因此,你最终会得到一个一扔和一个额外的onItemClick()。我认为这是由于Android将在每一个按钮后,一个项目,点击事件不管多远的方式向用户提出他的手指。为了补救而不是注册一个通常的 OnItemClickListener(),我提供我自己的方法这一点, myOnItemClick()。然后,我重写 SimpleOnGestureListener.onSingleTapUp()的方法,这样,当手指向上,此方法将手动调用myOnItemClick()。

到目前为止,这种方法为我工作正常。无抱怨: - )

 公共类PracticeActivity扩展ListActivity {

    私人诠释REL_SWIPE_MIN_DISTANCE;
    私人诠释REL_SWIPE_MAX_OFF_PATH;
    私人诠释REL_SWIPE_THRESHOLD_VELOCITY;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        //由于paiego指出,最好是使用密度感知测量。
        DisplayMetrics DM = getResources()getDisplayMetrics()。
        REL_SWIPE_MIN_DISTANCE =(int)的(120.0f * dm.densityDpi / 160.0f + 0.5);
        REL_SWIPE_MAX_OFF_PATH =(int)的(250.0f * dm.densityDpi / 160.0f + 0.5);
        REL_SWIPE_THRESHOLD_VELOCITY =(int)的(200.0f * dm.densityDpi / 160.0f + 0.5);

        ListView的LV = getListView();
        lv.setAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,
            m_Starbucks));

        最后GestureDetector gestureDetector =新GestureDetector(新MyGestureDetector());
        View.OnTouchListener gestureListener =新View.OnTouchListener(){
            公共布尔onTouch(视图V,MotionEvent事件){
                返回gestureDetector.onTouchEvent(事件);
            }};
        lv.setOnTouchListener(gestureListener);

        //长按依然工作在通常的方式。
        lv.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){
            公共布尔onItemLongClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                字符串str = MessageFormat.format(长项点击= {0,}号,位置);
                Toast.makeText(PracticeActivity.this,STR,Toast.LENGTH_SHORT).show();
                返回true;
            }
        });
    }

    //不要使用LitView.setOnItemClickListener()。相反,我重写
    // SimpleOnGestureListener.onSingleTapUp()方法,它会调用这个方法时,
    //检测到自来水事件的方式。
    私人无效myOnItemClick(INT位置){
        字符串str = MessageFormat.format(项点击= {0,}号,位置);
        Toast.makeText(这一点,STR,Toast.LENGTH_SHORT).show();
    }

    私人无效onLTRFling(){
        Toast.makeText(这一点,左向右甩,Toast.LENGTH_SHORT).show();
    }

    私人无效onRTLFling(){
        Toast.makeText(这一点,右到左一扔,Toast.LENGTH_SHORT).show();
    }

    类MyGestureDetector扩展SimpleOnGestureListener {

        //检测单点击和打电话给我自己的处理程序。
        @覆盖
        公共布尔onSingleTapUp(MotionEvent E){
            ListView的LV = getListView();
            INT POS = lv.pointToPosition((int)的e.getX(),(int)的e.getY());
            myOnItemClick(POS);
            返回false;
        }

        @覆盖
        公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
            如果(Math.abs(e1.getY() -  e2.getY())> REL_SWIPE_MAX_OFF_PATH)
                返回false;
            如果(e1.getX() -  e2.getX()> REL_SWIPE_MIN_DISTANCE&安培;&安培;
                Math.abs(velocityX)> REL_SWIPE_THRESHOLD_VELOCITY){
                onRTLFling();
            }否则,如果(e2.getX() -  e1.getX()> REL_SWIPE_MIN_DISTANCE和放大器;&安培;
                Math.abs(velocityX)> REL_SWIPE_THRESHOLD_VELOCITY){
                onLTRFling();
            }
            返回false;
        }

    }

    私有静态最后的String [] m_Starbucks = {
        拿铁,卡布奇诺,焦糖玛奇朵,美式,摩卡,白摩卡,
        摩卡瓦伦西亚,肉桂香料摩卡,太妃螺母拿铁,ES preSSO
        ES preSSO玛奇朵,ES preSSO精读奶
    };
}
 
Android开发学习 列表视图 ListView

hi i have to add a gesture to my listview,i want to implement the same functionality of contact application. when i left swipe it should send a message,right swipe it should call. can anyone help me how to do those gesture detection... i have implemented it in various other views... but i couldn't do for listView... i don't what going worng... my code is`

/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    // Create an array of Strings, that will be put to our ListActivity
    String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
    // Create an ArrayAdapter, that will actually make the Strings above
    // appear in the ListView
    this.setListAdapter(new ArrayAdapter<String>(this, R.id.MyList, names));
    gestureListener = new ListView.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };      
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();
    Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event))
        return true;
    else
        return false;
}

class MyGestureDetector extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        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) {
                Context ctx = getApplicationContext();
                CharSequence txt = "Right to Left Swipe";
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(ctx, txt, duration);
                toast.show();
                Toast.makeText(this.getItem(lv.pointToPosition((int)e1.getX(),(int) e1.getY())));
                // return super.onFling();                                          
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Context ctx = getApplicationContext();
                CharSequence txt = "Left to Right Swipe";
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(ctx, txt, duration);
                toast.show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}

解决方案

Based on answers from post [#937313][1] (thanks go to gav and paiego), I whipped up the following code to recognize simple gestures (horizontal swipes) on ListView.

However, after a fling operation, the ListView's onItemClick() listener will be called too! Therefore, you end up with a fling and an extra onItemClick(). I think this is because Android sends an item-click event on every button-up, no matter how far way the user has moved his finger. To remedy this, instead of registering a usual OnItemClickListener(), I provided my own method myOnItemClick(). Then I override the SimpleOnGestureListener.onSingleTapUp() method, so that when the finger is up, this method will call myOnItemClick() manually.

So far this method works fine for me. No complaints :-).

public class PracticeActivity extends ListActivity {

    private int REL_SWIPE_MIN_DISTANCE; 
    private int REL_SWIPE_MAX_OFF_PATH;
    private int REL_SWIPE_THRESHOLD_VELOCITY;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // As paiego pointed out, it's better to use density-aware measurements. 
        DisplayMetrics dm = getResources().getDisplayMetrics();
        REL_SWIPE_MIN_DISTANCE = (int)(120.0f * dm.densityDpi / 160.0f + 0.5); 
        REL_SWIPE_MAX_OFF_PATH = (int)(250.0f * dm.densityDpi / 160.0f + 0.5);
        REL_SWIPE_THRESHOLD_VELOCITY = (int)(200.0f * dm.densityDpi / 160.0f + 0.5);

        ListView lv = getListView();
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
            m_Starbucks));

        final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
        View.OnTouchListener gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event); 
            }};
        lv.setOnTouchListener(gestureListener);

        // Long-click still works in the usual way.
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                String str = MessageFormat.format("Item long clicked = {0,number}", position);
                Toast.makeText(PracticeActivity.this, str, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    // Do not use LitView.setOnItemClickListener(). Instead, I override 
    // SimpleOnGestureListener.onSingleTapUp() method, and it will call to this method when
    // it detects a tap-up event.
    private void myOnItemClick(int position) {
        String str = MessageFormat.format("Item clicked = {0,number}", position);
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    private void onLTRFling() {
        Toast.makeText(this, "Left-to-right fling", Toast.LENGTH_SHORT).show();
    }

    private void onRTLFling() {
        Toast.makeText(this, "Right-to-left fling", Toast.LENGTH_SHORT).show();
    }

    class MyGestureDetector extends SimpleOnGestureListener{ 

        // Detect a single-click and call my own handler.
        @Override 
        public boolean onSingleTapUp(MotionEvent e) {
            ListView lv = getListView();
            int pos = lv.pointToPosition((int)e.getX(), (int)e.getY());
            myOnItemClick(pos);
            return false;
        }

        @Override 
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
            if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH) 
                return false; 
            if(e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE && 
                Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) { 
                onRTLFling(); 
            }  else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE && 
                Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) { 
                onLTRFling(); 
            } 
            return false; 
        } 

    } 

    private static final String[] m_Starbucks = {
        "Latte", "Cappuccino", "Caramel Macchiato", "Americano", "Mocha", "White Mocha", 
        "Mocha Valencia", "Cinnamon Spice Mocha", "Toffee Nut Latte", "Espresso",
        "Espresso Macchiato", "Espresso Con Panna"
    };
}