2011-05-27 23 views
8

Me gustaría tener en mi aplicación el mismo comportamiento de la aplicación de contactos nativos. Específicamente, me gustaría implementar el deslizamiento hacia la derecha para llamar y deslizar hacia la izquierda para el texto. Tengo un ListView, he configurado el arrayAdapter y he implementado el detector de gestos para el onFlingMethod. I interceptar correctamente el lado del deslizamiento y puedo lauch la aplicación de llamada. Necesito poner el número de artículo (y otra información) en la intención, así que necesito obtener el artículo. Aquí mi código.OnFling en un ListView, Get the swiped Información del artículo

public class Contacts extends Activity implements OnGestureListener { 

    private static final String TAG = "[Contacts]"; 
    ArrayList <ContactInfo> mData; 

    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    /** Called when the activity is first created. */ 
    private GestureDetector detector = new GestureDetector(this); 
    Button btnContacts; 
    Button btnProfile; 
    Button btnSettings; 
    ImageButton btnStatus; 
    HandleServer cubeServer; 
    Button slideHandleButton; 
    SlidingDrawer slidingDrawer; 
    String filter = "n"; 
    ArrayAdapter <ContactInfo> adapter; 
    ListView listView; 


    public boolean onCreateOptionsMenu(Menu menu) { 

     return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     return true; 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact); 

     listView = (ListView) findViewById(R.id.arrayList); 
     listView.setCacheColorHint(R.color.white); 

     mData = getContact(); 

     adapter = new ArrayAdapter <ContactInfo> (this.getApplicationContext(), R.layout.row, R.id.nome, mData) { 
      public View getView(final int position, View convertView, ViewGroup parent) { 
       ViewHolder viewHolder = null; 
       if (convertView == null) { 

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(R.layout.row, null); 
        viewHolder = new ViewHolder(); 
        final ContactInfo item = getItem(position); 

        viewHolder.name.setText(item.getName()); 


        convertView.setClickable(true); 

        OnClickListener myClickListener = new OnClickListener() { 

         public void onClick(View v) { 
          Intent intent = new Intent(v.getContext(), ContactTabs.class); 
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          //Log.i(TAG,"id value:"+item.getId()); 
          intent.putExtra("id", item.getId()); 
          intent.putExtra("name", item.getName()); 
          //aggiungi quello che serve per gli extra ed i task 
          intent.putExtra("calendar", item.getCalendarDraw()); 
          intent.putExtra("gmail", item.getGmailDraw()); 
          intent.putExtra("operator", item.getOperatorDraw()); 
          intent.putExtra("imgStatus", item.getImgStatusDraw()); 
          startActivity(intent); 
         } 
        }; 

        convertView.setOnClickListener(myClickListener); 

        convertView.setOnTouchListener(new OnTouchListener() { 

         public boolean onTouch(View view, MotionEvent e) { 
          detector.onTouchEvent(e); 
          return false; 
         } 
        }); 

        return convertView; 
       } 
      }; 

      listView.setAdapter(adapter); 
     } 

     public boolean onDown(MotionEvent e) { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     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) { 
        //CallNativeApp cna = new CallNativeApp(getApplicationContext()); 
        //cna.sendSms("11111", ""); 
        Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show(); 
       } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        //CallNativeApp cna = new CallNativeApp(getApplicationContext()); 
        //cna.call("1111"); 

        Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show(); 
       } 
      } catch (Exception e) { 
       // nothing 
      } 

      return true; 
     } 
     public void onLongPress(MotionEvent e) { 
      // TODO Auto-generated method stub 

     } 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
      float distanceY) { 
      // TODO Auto-generated method stub 
      return true; 
     } 
     public void onShowPress(MotionEvent e) { 
      // TODO Auto-generated method stub 

     } 
     public boolean onSingleTapUp(MotionEvent e) { 
      // TODO Auto-generated method stub 
      return true; 
     } 

    } 
} 
+0

¿Cuál es el problema que enfrenta aquí? ¿Está funcionando como se esperaba? – Ads

Respuesta

7

Resuelvo este problema con el método ListView.pointToPosition().

Aquí mi fragmento de código:

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, 
    float velocityX, float velocityY) { 

    if (e2.getX() - e1.getX() > MOVE) { 

     int id = list.pointToPosition((int) e1.getX(), (int) e1.getY()); 
     Reminder temp = (Reminder) adapter.getItem((id)); 
     return true; 
    } 

    return false; 
} 

En primer lugar, se obtiene el rowID de list.pointToPosition((int) e1.getX(), (int) e1.getY()); y hacer con ella lo que quiera. En mi caso, Recordatorio es el tipo de objetos en mi adaptador de matriz personalizado para la lista.

0

yo asumiría la forma de hacerlo sería añadir el GestureListener a la fila, como lo hace con el OnClickListener, en lugar de la actividad.

Si tiene una lista grande, eso significaría una gran cantidad de GestureListeners y podría afectar el rendimiento. En ese caso, será mejor que calcule qué vista se ha cambiado utilizando la posición de deslizamiento y las vistas.

0

fin llegué solución de llegar al índice de golpe a la izquierda oa la derecha en la Lista Vista .... @ Override onFling pública booleano (MotionEvent e1, e2 MotionEvent, flotar velocityX, flotar velocityY) {

 boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         int id = list.pointToPosition((int) e1.getX(), (int) e1.getY()); 
          task = (TaskClass) adapter.getItem((id)); 
         Log.e("Result", "Result..."+task.toString()); 
         onSwipeRight(task.milestonetask); 
        } else { 
         int id = list.pointToPosition((int) e1.getX(), (int) e1.getY()); 
          task = (TaskClass) adapter.getItem((id)); 
         onSwipeLeft(task.milestonetask); 
        } 
       } 
      } else { 
       if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffY > 0) { 
         onSwipeBottom(); 
        } else { 
         onSwipeTop(); 
        } 
       } 
      } 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
Cuestiones relacionadas