在子片段内未检测到触摸
当我将侦听器附加到片段的子片段时,我遇到了onTouchListener问题。 我尝试了很多其他解决方案,但似乎听众不工作。 手势检测器代码取自这里:在网格布局上进行手势检测
main_activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/home_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/home_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
HomeFragment.java膨胀布局home_fragment.xml并调用其子片段ChildFragment.java
home_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/functionPad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/gradient_home_background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:orientation="vertical"
android:id="@+id/row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bgPanelResult">
<TextView
android:id="@+id/panelResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
</LinearLayout>
<FrameLayout
android:id="@+id/keypadFrame"
android:layout_alignParentBottom="true"
android:layout_below="@+id/row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
ChildFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.child_fragment, container, false);
// Gesture detection
gestureDetector = new GestureDetector(getActivity(), new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
rootView.findViewById(R.id.keypadSimple).setOnTouchListener(gestureListener);
return rootView;
}
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getActivity(), "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getActivity(), "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
链接地址: http://www.djcxy.com/p/91321.html