我想检测具有特定布局的onLongPress事件。在我的xL LinearLayout中,在RelativeLayout中。我想只在LinearLayout中获取onLongTouch。我尝试使用onTouchEvent这很好用,但onLongPress工作在孔布局上包括RelativeLayout。如果我将return mDetector.onTouchEvent(event);
放在myLinear.setOnTouchListener
中。它会变成单个Touch.Please帮我解决这个问题。任何回复我都非常感谢。
activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:text="Show Text"
android:textStyle="bold"
android:textSize="@dimen/abc_text_size_title_material"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="@+id/myLinear"
android:layout_below="@+id/txtView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/floorimg">
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout myLinear;
private GestureDetectorCompat mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLinear = (LinearLayout)findViewById(R.id.myLinear);
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
myLinear.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
});
}
/* @Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}*/
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(getBaseContext(),"Long press called",Toast.LENGTH_SHORT).show();
super.onLongPress(e);
}
}
}
答案 0 :(得分:0)
您可能希望将OnLongClickListener
与LinearLayout一起使用:
myLinear.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long press called", Toast.LENGTH_SHORT).show();
return true;
}
});