Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
listview android中的手势_Android_Flip_Gesture_Swipe - Fatal编程技术网

listview android中的手势

listview android中的手势,android,flip,gesture,swipe,Android,Flip,Gesture,Swipe,嗨,我必须向我的listview添加一个手势,我想实现联系人应用程序的相同功能。 当我向左滑动时,它会发送消息,向右滑动时,它会呼叫。谁能帮我做手势检测。。。我已经在各种其他视图中实现了它。。。但是我不能为listView做任何事情。。。我不知道发生了什么事。。。 我的代码是` /** Called when the activity is first created. */ public void onCreate(Bundle icicle) { super.onCreate(ici

嗨,我必须向我的listview添加一个手势,我想实现联系人应用程序的相同功能。 当我向左滑动时,它会发送消息,向右滑动时,它会呼叫。谁能帮我做手势检测。。。我已经在各种其他视图中实现了它。。。但是我不能为listView做任何事情。。。我不知道发生了什么事。。。 我的代码是`

/** 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;
    }
}
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
//创建一个字符串数组,将其放入我们的ListActivity
字符串[]名称=新字符串[]{“Linux”、“Windows7”、“Eclipse”、“Suse”、“Ubuntu”、“Solaris”、“Android”、“iPhone”};
//创建一个ArrayAdapter,它将实际生成上面的字符串
//显示在列表视图中
this.setListAdapter(新的ArrayAdapter(this,R.id.MyList,names));
gestureListener=新建ListView.OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
if(手势检测器onTouchEvent(事件)){
返回true;
}
返回false;
}
};      
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
//获取已单击的项目
对象o=this.getListAdapter().getItem(位置);
字符串关键字=o.toString();
Toast.makeText(您选择的“+”关键字,Toast.LENGTH_LONG.show();
}
@凌驾
公共布尔onTouchEvent(运动事件){
if(手势检测器onTouchEvent(事件))
返回true;
其他的
返回false;
}
类MyGestureDetector扩展了SimpleOnGestureListener{
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
试一试{
if(Math.abs(e1.getY()-e2.getY())>swip\u MAX\u OFF\u路径){
返回false;
//从右向左滑动
}
if(e1.getX()-e2.getX()>滑动最小距离和&Math.abs(速度x)>滑动阈值速度){
Context ctx=getApplicationContext();
CharSequence txt=“从右向左滑动”;
int duration=Toast.LENGTH\u LONG;
Toast Toast=Toast.makeText(ctx,txt,duration);
toast.show();
Toast.makeText(this.getItem(lv.pointToPosition((int)e1.getX(),(int)e1.getY());
//返回super.onFling();
}else if(e2.getX()-e1.getX()>swip\u MIN\u DISTANCE&&Math.abs(velocityX)>swip\u THRESHOLD\u VELOCITY){
Context ctx=getApplicationContext();
CharSequence txt=“从左向右滑动”;
int duration=Toast.LENGTH\u LONG;
Toast Toast=Toast.makeText(ctx,txt,duration);
toast.show();
}
}捕获(例外e){
//没什么
}
返回false;
}
}

根据post[937313][1]的答案(感谢gav和paiego),我快速编写了以下代码,以识别ListView上的简单手势(水平滑动)

但是,在一个fling操作之后,ListView的
onItemClick()
监听器也将被调用!因此,您最终会得到一次放纵和一次额外的mClick()。我认为这是因为Android在每个按钮上都会发送一个项目点击事件,不管用户的手指移动了多远。为了解决这个问题,我没有注册一个普通的
OnItemClickListener()
,而是提供了自己的方法
myOnItemClick()
。然后我重写了
SimpleOnGestureListener.onSingleTapUp()
方法,这样当手指向上时,该方法将手动调用myOnItemClick()

到目前为止,这种方法对我很有效。无投诉:-)

公共课堂实践活动扩展了ListActivity{
私人内部相对滑动距离;
私用int REL_刷出_MAX_路径;
专用int REL_滑动阈值_速度;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//正如paiego指出的,最好使用密度感知测量。
DisplayMetrics dm=getResources().getDisplayMetrics();
相对滑动距离=(整数)(120.0f*dm.densityDpi/160.0f+0.5);
相对滑动路径=(整数)(250.0f*dm.densityDpi/160.0f+0.5);
相对滑动阈值速度=(整数)(200.0f*dm.densityDpi/160.0f+0.5);
ListView lv=getListView();
lv.setAdapter(新的ArrayAdapter)(这个,android.R.layout.simple\u列表\u项目\u 1,
m_星巴克);
最终GestureDetector GestureDetector=新的GestureDetector(新的MyGestureDetector());
View.OnTouchListener gestureListener=新建视图。OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
返回gestureDetector.onTouchEvent(事件);
}};
lv.setOnTouchListener(手势Listener);
//长点击仍然以通常的方式工作。
lv.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
String str=MessageFormat.format(“长时间单击的项={0,number}”,位置);
Toast.makeText(PracticeActivity.this,str,Toast.LENGTH_SHORT).show();
返回true;
}
});
}
//不要使用LitView.setOnItemClickListener()。而是我重写
//方法,它将在
//它检测到一个窃听事件。
私有void myOnItemClick(int位置){
String str=MessageFormat.format(“单击的项目={0,编号}”,位置);
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
私有无效ontrfling()
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"
    };
}