2013-08-26 70 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是什么,所以它可能会做同样的事情