2013-08-26 72 views
0

請看看下面的代碼OnTouchListener不解僱

XML代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".ParagraphReader" > 

    <ScrollView 
     android:id="@+id/paragraph_reader_scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
     <TextView 
      android:id="@+id/paragraph_reader_txt" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="30" 
      android:singleLine="false" 
      android:enabled="true" 
      > 


     </TextView> 

    </ScrollView> 






</RelativeLayout> 

的Java代碼

package k.k; 

import java.util.ArrayList; 
import java.util.List; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View.OnTouchListener; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ParagraphReader extends Activity { 

    private TextView paraText; 
    private DatabaseConnector database; 
    private List<String>paraList; 
    private int currentQuestion; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_paragraph_reader); 

     paraText = (TextView)findViewById(R.id.paragraph_reader_txt); 
     paraText.setOnTouchListener(paraSwiped); 

     paraList = new ArrayList<String>(); 
     database = DatabaseHandler.getInstance(this); 

     //Get the Paragraph list 
     int listNumber = getIntent().getIntExtra("PARAGRAPH_LIST", 0); 

     Toast.makeText(this, "Selected Paragraph: "+listNumber, Toast.LENGTH_LONG).show(); 

     paraList = database.getParagraphList(listNumber); 
     Toast.makeText(this, "ParaList size "+paraList.size(), Toast.LENGTH_LONG).show(); 

     //Toast.makeText(this, "Size: "+paraList.size(), Toast.LENGTH_LONG).show(); 
     paraText.setText(paraList.get(0)); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.paragraph_reader, menu); 
     return true; 
    } 


    //The Event Handler for the Paragraph Text holder 
    OnTouchListener paraSwiped = new OnSwipeTouchListener() 
    { 
     public boolean onSwipeRight() 
     { 
      Toast.makeText(ParagraphReader.this, "Right: "+paraList.size(), Toast.LENGTH_SHORT).show(); 


      return true; 
     } 

      public boolean onSwipeLeft() 
      { 
       Toast.makeText(ParagraphReader.this, "Left: "+paraList.size(), Toast.LENGTH_SHORT).show(); 

       return true; 
      } 


    }; 

} 

在這裏,你可以看到我已經實現一個OnTouchListenerTextView。以下是OnTouchListener類的代碼。此代碼由SO成員之一構建。

package k.k; 
import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

    public boolean onTouch(final View v, final MotionEvent event) { 
     return gestureDetector.onTouchEvent(event); 

    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return super.onDown(e); 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float 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) { 
          result = onSwipeRight(); 
         } else { 
          result = onSwipeLeft(); 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          result = onSwipeBottom(); 
         } else { 
          result = onSwipeTop(); 
         } 
        } 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 

    public boolean onSwipeRight() { 
     return false; 
    } 

    public boolean onSwipeLeft() { 
     return false; 
    } 

    public boolean onSwipeTop() { 
     return false; 
    } 

    public boolean onSwipeBottom() { 
     return false; 
    } 
} 

我只需要左右滑動,但由於某種原因,Swipong不起作用。是的,它不工作,這意味着什麼都沒有發生。我真的不明白如何解決這個問題。我知道由SO成員開發的代碼正在工作,因爲我正在另一項活動中使用它。

求助。

+0

您可能會嘗試使用android:clickable =「true」使文本視圖可點擊。不確定這會解決問題,但它是值得一試的。我不確定android:enabled是什麼,所以它可能會做同樣的事情。 – user2483079

+0

@ user2483079:是的,你是對的。請提供評論作爲答案 –

回答

1

您可以嘗試使用android:clickable =「true」使文本視圖可點擊。不確定這會解決問題,但它是值得一試的。我不確定android:enabled是什麼,所以它可能會做同樣的事情