从片段中替换片段会导致片段内容重叠
Replacing fragment from fragment causes Fragments content to overlap
我在片段中有一个 ListView。当我单击列表中的项目时,我想 "start" 使用替换的新片段。这是结果。我应该只得到 BUTTON:
这是在 onItemClick:
里面
Fragment nextFrag= new VitaminFragment();
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
transaction.replace(vitamin_fragment.getId(), nextFrag);
transaction.addToBackStack(null);
transaction.commit();
在 VitaminFragment 的 onCreateView 中:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("created", "vitamin_fragment_2");
return inflater.inflate(R.layout.supplement_properties_layout, container, false);
}
和 VitaminFragment XML (supplement_properties_layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="48dip" />
<android.support.v4.view.ViewPager
android:id="@+id/supplements_properties_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tabs" />
</RelativeLayout>
VitaminWiki 的布局以编程方式创建:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View result = initComponents();
.....
.....
}
private RelativeLayout initComponents() {
vitamin_fragment = new RelativeLayout(getActivity());
vitamin_fragment.setId(View.generateViewId());
RelativeLayout.LayoutParams layout_57 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
vitamin_fragment.setLayoutParams(layout_57);
ListView listView = new ListView(getActivity());
......
vitamin_fragment.addView(listView);
com.astuetz.PagerSlidingTabStrip p = new PagerSlidingTabStrip(getActivity());
...........
p.setLayoutParams(params);
vitamin_fragment.addView(p);
ViewPager viewPager = new ViewPager(getActivity());
.........
vitamin_fragment.addView(viewPager);
return vitamin_fragment;
}
最后HERE就是附上片段的Activity
FragmentTransaction 的 replace() 方法简单地从指定容器中删除所有片段,然后调用 add()。因此,如果指定的容器为空,replace() 只是在当前视图之上添加一个片段。
您应该 post 您的 VitaminFragment 的 XML 文件,但我想说为您的 VitaminFragment 添加背景,您应该没问题。
并且不要忘记将片段的主布局设置为可点击。否则,任何点击按钮以外的东西都将通过 Fragment,并可能点击您 ListView 上的项目。
您的 Fragment 应如下所示:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true" >
编辑:如果您希望 replace() 方法起作用并实际从 activity 中删除 ListView,请确保在用于在第一个片段中添加第一个片段的同一容器上调用它地方。此外,第一个片段需要动态添加。当我说动态添加时,我的意思是使用 FragmentTransaction.add() 方法。
我在片段中有一个 ListView。当我单击列表中的项目时,我想 "start" 使用替换的新片段。这是结果。我应该只得到 BUTTON:
这是在 onItemClick:
里面Fragment nextFrag= new VitaminFragment();
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
transaction.replace(vitamin_fragment.getId(), nextFrag);
transaction.addToBackStack(null);
transaction.commit();
在 VitaminFragment 的 onCreateView 中:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("created", "vitamin_fragment_2");
return inflater.inflate(R.layout.supplement_properties_layout, container, false);
}
和 VitaminFragment XML (supplement_properties_layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="48dip" />
<android.support.v4.view.ViewPager
android:id="@+id/supplements_properties_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tabs" />
</RelativeLayout>
VitaminWiki 的布局以编程方式创建:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View result = initComponents();
.....
.....
}
private RelativeLayout initComponents() {
vitamin_fragment = new RelativeLayout(getActivity());
vitamin_fragment.setId(View.generateViewId());
RelativeLayout.LayoutParams layout_57 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
vitamin_fragment.setLayoutParams(layout_57);
ListView listView = new ListView(getActivity());
......
vitamin_fragment.addView(listView);
com.astuetz.PagerSlidingTabStrip p = new PagerSlidingTabStrip(getActivity());
...........
p.setLayoutParams(params);
vitamin_fragment.addView(p);
ViewPager viewPager = new ViewPager(getActivity());
.........
vitamin_fragment.addView(viewPager);
return vitamin_fragment;
}
最后HERE就是附上片段的Activity
FragmentTransaction 的 replace() 方法简单地从指定容器中删除所有片段,然后调用 add()。因此,如果指定的容器为空,replace() 只是在当前视图之上添加一个片段。
您应该 post 您的 VitaminFragment 的 XML 文件,但我想说为您的 VitaminFragment 添加背景,您应该没问题。
并且不要忘记将片段的主布局设置为可点击。否则,任何点击按钮以外的东西都将通过 Fragment,并可能点击您 ListView 上的项目。
您的 Fragment 应如下所示:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true" >
编辑:如果您希望 replace() 方法起作用并实际从 activity 中删除 ListView,请确保在用于在第一个片段中添加第一个片段的同一容器上调用它地方。此外,第一个片段需要动态添加。当我说动态添加时,我的意思是使用 FragmentTransaction.add() 方法。