从 OnClick 侦听器扩展布局
Inflate a layout from an OnClick listener
我有一个从片段延伸的 class,我想在该片段中创建一个表单。为此,我需要在不移出片段的情况下扩充不同的布局。 (描述图片和代码)
When clicking in "Next", the app must go to the "step 2" BUT keep on the "Denunciar" tab from upside
我试了一下,不知道哪里错了
public class Tab1 extends Fragment implements View.OnClickListener{
ViewGroup container;
LayoutInflater inflater;
View v;
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.tab_1,container,false);
Button buttonTab1 = (Button) v.findViewById(R.id.botonTab1);
buttonTab1.setOnClickListener(this);
this.container = container;
this.inflater = inflater;
return v;
}
@Override
public void onClick(View v) {
v = inflater.inflate(R.layout.tab_2,container,false);
}
}
首先是行不通的,因为,您的片段只会在 onCreateView()
方法上将视图附加到简而言之,并且一旦视图附加到 window 然后对于新视图,您必须再次重绘视图.再一次如果你可以根据你应用的条件跟踪片段的状态,你可以分离并附加片段,这样它会再次创建片段,这会导致现在在某些条件下再次调用 onCreateView()
你可以更改布局文件.
要处理此问题,您可以在 Fragments 中使用 Nested Fragments 或 ViewPager。
一些参考资料:
http://developer.android.com/about/versions/android-4.2.html#NestedFragments
https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Nested
我有一个从片段延伸的 class,我想在该片段中创建一个表单。为此,我需要在不移出片段的情况下扩充不同的布局。 (描述图片和代码)
When clicking in "Next", the app must go to the "step 2" BUT keep on the "Denunciar" tab from upside
我试了一下,不知道哪里错了
public class Tab1 extends Fragment implements View.OnClickListener{
ViewGroup container;
LayoutInflater inflater;
View v;
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.tab_1,container,false);
Button buttonTab1 = (Button) v.findViewById(R.id.botonTab1);
buttonTab1.setOnClickListener(this);
this.container = container;
this.inflater = inflater;
return v;
}
@Override
public void onClick(View v) {
v = inflater.inflate(R.layout.tab_2,container,false);
}
}
首先是行不通的,因为,您的片段只会在 onCreateView()
方法上将视图附加到简而言之,并且一旦视图附加到 window 然后对于新视图,您必须再次重绘视图.再一次如果你可以根据你应用的条件跟踪片段的状态,你可以分离并附加片段,这样它会再次创建片段,这会导致现在在某些条件下再次调用 onCreateView()
你可以更改布局文件.
要处理此问题,您可以在 Fragments 中使用 Nested Fragments 或 ViewPager。
一些参考资料:
http://developer.android.com/about/versions/android-4.2.html#NestedFragments
https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Nested