在单个活动中,我用线性布局替换多个片段。手段,现在我的“容器”布局保存碎片。我尝试在我的后续框架布局和线性布局上设置View.onTouchListener但是没有工作。为什么呢?
以下是我的MainActivity布局xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
以下方法我在MainActivity中设置onTouchListener:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mainLayout = (FrameLayout) findViewById(R.id.main_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container);
mainLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
MyToast.showToast("inner main");
return true;
}
});
containerLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
MyToast.showToast("inner main");
return true;
}
});
FragmentTransaction tran = getSupportFragmentManager().beginTransaction();
tran.replace(R.id.container, new RegistrationFragment(), Keys.TAG_REGISTRATION_FRAGMENT);
tran.commit();
}