[英]The same code is not working in a different activity
I have this code, supposed to catch the swipe events in one of my activities: 我有这段代码,应该可以在我的一项活动中捕捉滑动事件:
private float x1,x2;
static final int MIN_DISTANCE = 150;
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show ();
this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
}
// Right to left swipe action
else
{
Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show ();
}
}
else
{
// TO DO
}
break;
}
return super.onTouchEvent(event);
}
Now I just moved the same working code in another activity and it is not showing the toast message when I swipe left or right. 现在,我只是在另一个活动中移动了相同的工作代码,但向左或向右滑动时并没有显示吐司消息。 What can be causing the issue? 是什么原因引起的? I know that I'm missing something really small, but I can not spot it at the momment. 我知道我缺少一些很小的东西,但是我无法在瞬间发现它。
EDIT the non working activity: 编辑非工作活动:
public class BulgarianSayings extends Activity {
private float x1,x2;
static final int MIN_DISTANCE = 150;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bulgarian_sayings);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bulgarian_sayings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bulgarian_sayings, container, false);
return rootView;
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show ();
this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
}
// Right to left swipe action
else
{
Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show ();
}
}
else
{
// consider as something else - a screen tap for example
}
break;
}
return super.onTouchEvent(event);
}
}
You Should append the onTouchListener on a view to make it work. 您应该在视图上附加onTouchListener以使其起作用。
For example If you want to implementing this on whole view You could do this: 例如,如果要在整个视图上实施此操作,则可以执行以下操作:
// First make an id for your View (LinearLayout, RelativeLayout, etc.)
// and then decaler it as a view.
View view = findViewById(R.id.rellayout);
// then append onTouchListener on it
view.setOnTouchListener(Your OnTouchListener Class or Method);
Other way is to work like this in top of activity: 另一种方法是在活动顶部像这样工作:
public class BulgarianSayings extends Activity implements OnTouchListener {
//Your Other parts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.