我正在尝试创建一个自定义的LinearLayout视图(称为myView),该视图具有自己的用于flings的GestureDetector。此LinearLayout放置在主布局中,我想检测此自定义LinearLayout上的fling / touch。
我已尝试在这些论坛上遵循所有建议,包括触摸触摸,调度和拦截方法。但是,我甚至无法获得任何这些方法甚至无法启动(我没有看到我的调试输出)。有人可以告诉我哪里出错了吗?以下是相关的代码段。
自定义LinearLayout:
public class MyView extends LinearLayout {
private final TextView mText;
private final ImageView mImage;
private GestureDetector mGestureDetector;
private static final String DEBUG_TAG = "Debug";
public CardView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.card_view, this);
mText = (TextView) findViewById(R.id.my_text);
mImage = (ImageView) findViewById(R.id.my_picture);
setupGestureDetector();
}
// Set up GestureDetector
private void setupGestureDetector() {
mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
//@Override
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.e(DEBUG_TAG, "Detected Fling!");
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// TODO - delegate the touch to the gestureDetector
Log.e(DEBUG_TAG, "Intercepted touch");
return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
// TODO - delegate the touch to the gestureDetector
Log.e(DEBUG_TAG, "Dispatched touch");
return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO - delegate the touch to the gestureDetector
Log.e(DEBUG_TAG, "Detected on Touch");
return mGestureDetector.onTouchEvent(event);
}
}
主要活动:
private static final String DEBUG_TAG = "Opine";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(DEBUG_TAG, "Started my app");
}
}
自定义LinearLayout的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/border">
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"/>
<ImageView
android:id="@+id/my_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
主要活动的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"
tools:context="${relativePackage}.${activityClass}" >
<com.pratikthaker.opine.MyView
android:id="@+id/view0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
答案 0 :(得分:0)
问题在于,您甚至没有将mGestureDetector
注册到LinearLayout
以获得onTouch
的某些权限,从而无法从日志中获得任何结果。
<强>解决方案强>
使用onTouchListener
参数在LinearLayout的MotionEvent
中注册,以便在触摸布局时获得一些响应。
<强>示例强>
private void setupGestureDetector() {
mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.e(DEBUG_TAG, "Detected Fling!");
return true;
}
});
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return false;
}
});
}
另一个问题:
<com.pratikthaker.opine.MyView
android:id="@+id/view0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
由于您正在包装其内容,因此MyView无法触摸,您宁愿触摸相对布局。您需要使用match_parent
属性。