2013-05-14 67 views
0

我正在嘗試實施手勢。最終,它將具有與原生Android聯繫人應用程序相同的功能,您將刷新該列表視圖並打開撥號程序。儘管如此,我仍然有一個問題讓所有的東西都100%到目前爲止,listview會像它應該加載的那樣,但是經過很多谷歌搜索和教程後,我想到了這一點,並需要幫助。實現Android手勢的問題

SimpleGestureFilter

public final static int SWIPE_UP = 1; 
public final static int SWIPE_DOWN = 2; 
public final static int SWIPE_LEFT = 3; 
public final static int SWIPE_RIGHT = 4; 

public final static int MODE_TRANSPARENT = 0; 
public final static int MODE_SOLID  = 1; 
public final static int MODE_DYNAMIC  = 2; 

private final static int ACTION_FAKE = -13; //just an unlikely number 
private int swipe_Min_Distance = 100; 
private int swipe_Max_Distance = 350; 
private int swipe_Min_Velocity = 100; 

private int mode  = MODE_DYNAMIC; 
private boolean running = true; 
private boolean tapIndicator = false; 

private Activity context; 
private GestureDetector detector; 
private SimpleGestureListener listener; 


public SimpleGestureFilter(Activity context,SimpleGestureListener sgl) { 

    this.context = context; 
    this.detector = new GestureDetector(context, this); 
    this.listener = sgl; 
} 

public void onTouchEvent(MotionEvent event){ 

    if(!this.running) 
    return; 

    boolean result = this.detector.onTouchEvent(event); 

    if(this.mode == MODE_SOLID) 
    event.setAction(MotionEvent.ACTION_CANCEL); 
    else if (this.mode == MODE_DYNAMIC) { 

    if(event.getAction() == ACTION_FAKE) 
     event.setAction(MotionEvent.ACTION_UP); 
    else if (result) 
     event.setAction(MotionEvent.ACTION_CANCEL); 
    else if(this.tapIndicator){ 
     event.setAction(MotionEvent.ACTION_DOWN); 
     this.tapIndicator = false; 
    } 

    } 
    //else just do nothing, it's Transparent 
} 

public void setMode(int m){ 
    this.mode = m; 
} 

public int getMode(){ 
    return this.mode; 
} 

public void setEnabled(boolean status){ 
    this.running = status; 
} 

public void setSwipeMaxDistance(int distance){ 
    this.swipe_Max_Distance = distance; 
} 

public void setSwipeMinDistance(int distance){ 
    this.swipe_Min_Distance = distance; 
} 

public void setSwipeMinVelocity(int distance){ 
    this.swipe_Min_Velocity = distance; 
} 

public int getSwipeMaxDistance(){ 
    return this.swipe_Max_Distance; 
} 

public int getSwipeMinDistance(){ 
    return this.swipe_Min_Distance; 
} 

public int getSwipeMinVelocity(){ 
    return this.swipe_Min_Velocity; 
} 


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

    final float xDistance = Math.abs(e1.getX() - e2.getX()); 
    final float yDistance = Math.abs(e1.getY() - e2.getY()); 

    if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance) 
    return false; 

    velocityX = Math.abs(velocityX); 
    velocityY = Math.abs(velocityY); 
     boolean result = false; 

    if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){ 
    if(e1.getX() > e2.getX()) // right to left 
    this.listener.onSwipe(SWIPE_LEFT); 
    else 
    this.listener.onSwipe(SWIPE_RIGHT); 

    result = true; 
    } 
    else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance){ 
    if(e1.getY() > e2.getY()) // bottom to up 
    this.listener.onSwipe(SWIPE_UP); 
    else 
    this.listener.onSwipe(SWIPE_DOWN); 

    result = true; 
    } 

    return result; 
} 

@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    this.tapIndicator = true; 
    return false; 
} 

@Override 
public boolean onDoubleTap(MotionEvent arg0) { 
    this.listener.onDoubleTap();; 
    return true; 
} 

@Override 
public boolean onDoubleTapEvent(MotionEvent arg0) { 
    return true; 
} 

@Override 
public boolean onSingleTapConfirmed(MotionEvent arg0) { 

    if(this.mode == MODE_DYNAMIC){  // we owe an ACTION_UP, so we fake an  
    arg0.setAction(ACTION_FAKE);  //action which will be converted to an ACTION_UP later.          
    this.context.dispatchTouchEvent(arg0); 
    } 

    return false; 
} 


    static interface SimpleGestureListener{ 
    void onSwipe(int direction); 
    void onDoubleTap(); 
} 

} 

DisplayCompanies.java

public class DisplayCompanies extends ListActivity { 
    //JSONArrays? 
    JSONArray directory; 
    private SimpleGestureFilter detector; 


    //JSON Node names 
    private static String TAG_ID = "id"; 
    private static String TAG_COMPANY= "organization"; 
    private static String TAG_PHONE= "number"; 
    private static String TAG_CATEGORY= "companies"; 

    private final static String url= "API URL BASE HERE"; 
    JSONObject json; 
    CompanyListJSONParser jParser = new CompanyListJSONParser(); 
    ArrayList<HashMap<String, String>> directoryList; 

    @SuppressLint("NewApi") 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.service); 

     directoryList = new ArrayList<HashMap<String, String>>(); 
     Request request = new Request(); 
     request.execute(); 


     // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // Show the Up button in the action bar. 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
      setTitle("Available Companies"); 

     } 
    }// end of onCreate Method 
    @SuppressWarnings("unused") 
    public class Request extends AsyncTask<String, Void, JSONObject> { 

     private static final int REGISTRATION_TIMEOUT = 3 * 1000; 
     private static final int WAIT_TIMEOUT = 30 * 1000; 
     private ProgressDialog dialog = 
       new ProgressDialog(DisplayCompanies.this); 


     protected void onPreExecute() { 
      dialog = new ProgressDialog(DisplayCompanies.this); 
      dialog.setMessage("Getting Available Companies... Please wait..."); 
      dialog.show(); 
     } 

     protected JSONObject doInBackground(String... params) { 
      String cat = null; 
      Bundle extras = getIntent().getExtras(); 
      if (extras != null) { 
       cat = extras.getString("catID"); 
      } 
      String fullUrl = url + cat; 
      json = jParser.getJSONfromURL(fullUrl); 

      return json; 

     } 

     protected void onPostExecute(JSONObject s) {   
      super.onPostExecute(s); 
      String name = null; 
      String id = null; 
      String phone = null; 
      dialog.dismiss(); 

      try { 
       directory = s.getJSONArray(TAG_CATEGORY); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      for(int i = 0; i < directory.length(); i++){; 
      try { 
       id = directory.getJSONObject(i).getString(TAG_ID); 
       name = directory.getJSONObject(i).getString(TAG_COMPANY); 
       phone = directory.getJSONObject(i).getString(TAG_PHONE); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      displayCatList(id, name, phone); 

      } 

     } 

    } 

    public void displayCatList(String id, String name, String phone){     
     //create new HashMap 
     HashMap<String,String> map = new HashMap<String, String>(); 

     //add each child node to HashMap key 
     map.put(TAG_ID, id); 
     map.put(TAG_COMPANY, name); 
     map.put(TAG_PHONE, phone); 

     //adding HashList to ArrarList 
     directoryList.add(map); 

     MySimpleAdapter adapter = new MySimpleAdapter(this, R.layout.list_item, android.R.id.text1, directoryList); 
     setListAdapter(adapter); 

     adapter.notifyDataSetChanged(); 
    } 



    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    public class MySimpleAdapter extends ArrayAdapter<HashMap<String, String>> { 

     List<HashMap<String, String>> listItems; 

     public MySimpleAdapter(Context context, int resource, 
       int textViewResourceId, List<HashMap<String, String>> objects) { 
      super(context, resource, textViewResourceId, objects); 
      listItems = objects; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      if(convertView == null) { 
       LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflator.inflate(R.layout.list_item, null); 
      } 

      TextView listName = (TextView) convertView.findViewById(R.id.listName); 

      listName.setTag(listItems.get(position).get(TAG_ID)); 
      listName.setText(listItems.get(position).get(TAG_COMPANY)); 

      //for use with onClick 
      listName.setOnClickListener(new OnClickListener() { 
       @Override 
       /** 
       * Method provides action when the selected item in list view is clicked. 
       */ 
       public void onClick(View v){ 
        Log.i("Click", "id # " + listItems.get(position).get(TAG_ID) + " Company: " + listItems.get(position).get(TAG_COMPANY)+ " Phone #: " + listItems.get(position).get(TAG_PHONE)); 
        //Intent DisplayCompanies = new Intent (DisplayServiceActivity.this, DisplayCompanies.class); 
        //DisplayServiceActivity.this.startActivity(DisplayCompanies); 
       } 
      }); 

      //Gesture or fling implementation 
      detector = new SimpleGestureFilter(this,this); 


      return convertView; 
     } 


     public boolean dispatchTouchEvent(MotionEvent me){ 
      detector.onTouchEvent(me); 
      return super.dispatchTouchEvent(me); 
     } 
     public void onSwipe(int direction) { 
      String str = ""; 

      switch (direction) { 

      case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right"; 
      break; 
      case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left"; 
      break; 
      case SimpleGestureFilter.SWIPE_DOWN : str = "Swipe Down"; 
      break; 
      case SimpleGestureFilter.SWIPE_UP : str = "Swipe Up"; 
      break; 

      } 
      Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); 
     } 

     public void onDoubleTap() { 
      Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show(); 
     } 

    } 

} 

} 

此時Eclipse將無法編譯,這些都是什麼下劃線:

public boolean dispatchTouchEvent(MotionEvent me){ 
      detector.onTouchEvent(me); 
      return super.dispatchTouchEvent(me); 
     } 

因爲:他法dispatchTouchEvent( MotionEvent)未定義爲類型ArrayAdapter>

而且

Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show(); 

因爲:該方法makeText(上下文,CharSequence中,INT)在類型Toast是不適用的參數(DisplayCompanies.MySimpleAdapter,字符串,整數)

任何幫助的這將是偉大的。提前謝謝你!

回答

0

您不斷製作新的手勢過濾器,並使用此代碼反覆將其分配到同一事物。

//Gesture or fling implementation 
    detector = new SimpleGestureFilter(this,this); 

你應該爲每個視圖手勢檢測,而不是一個用於ListView

+0

我只想把'探測器=新SimpleGestureFilter(這一點,這一點);在onCreate'方法中? – user1890328 2013-05-14 18:41:06