【问题标题】:How Can I launch event when user touch during 2 second on listview?当用户在列表视图上触摸 2 秒时如何启动事件?
【发布时间】:2015-05-29 01:57:26
【问题描述】:

当用户在列表视图上保持 2 秒时,我需要启动添加事件。之后振动设备并显示对话框询问“添加到收藏夹?”。

我正在尝试这个。

lv.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.AXIS_PRESSURE){

            long eventDuration = 
                    android.os.SystemClock.elapsedRealtime() 
                    - event.getDownTime();

        //Put up the Yes/No message box


        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        alert
        .setTitle("Service")
        .setMessage("Add to favorite?")
        //.setIcon(R.drawable.chile1)
        .setPositiveButton("Si", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {                    
                 Intent intent = new Intent(Activity.class);
                    startActivity(intent);

            }

        })
        .setNegativeButton("No", null)                      //Do nothing on no
        .show();

    }
        return false;   
    }

上面的代码显示 5 次对话框。

【问题讨论】:

标签: android android-listview


【解决方案1】:

你需要使用OnItemLongClickListener(),像这样:

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            alert.setTitle("Service")
                    .setMessage("Add to favorite?")
                    //.setIcon(R.drawable.chile1)
                    .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Activity.class);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton("No", null) //Do nothing on no
                    .show();
            return true;
        });

【讨论】:

    【解决方案2】:

    首先,您需要在 Manifest 文件中允许振动:

    <uses-permission android:name="android.permission.VIBRATE"/>
    

    然后长按你可以使用 mListView.setOnItemLongClickListener() 像这里:

    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> p, View v, int i, long id) {
            // Do Stuff
            return false;
        }
    });
    

    然后在Do Stuff上,您可以调用Vibrator,然后打开对话框要求添加到收藏夹。 振动器如下所示:

    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(200); // 200 miliseconds
    

    【讨论】:

      【解决方案3】:

      你可以检查下面的代码:

      private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
          @Override
          public boolean onTouch(final View view,
                                 final MotionEvent event) {
              switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      handler.postDelayed(mLongPressed,
                                          1000);
                      //This is where my code for movement is initialized to get original location.
                      break;
                  case MotionEvent.ACTION_UP:
                      handler.removeCallbacks(mLongPressed);
      
                      break;
                  case MotionEvent.ACTION_MOVE:
                      handler.removeCallbacks(mLongPressed);
                      //Code for movement here. This may include using a window manager to update the view
                      break;
              }
              return true;        }
      };
      
      
      
      //Put this into the class
      final Handler handler = new Handler();
      Runnable mLongPressed = new Runnable() {
          public void run() {
              //TODO :show dialog when hold 1s.
              // can you set time show dialog handler.postDelayed(mLongPressed, 1000);
      
          }
      };
      

      设置 OnTouch: lv.setOnTouchListener(onTouchListener);

      【讨论】:

        猜你喜欢
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多