解决android - Replacing fragment from fragment causes Fragments content to overlap
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
推荐:android Fragments:管理fragment
要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情: 1使用方法findF
I have a ListView inside a fragment. When i click an item on the List i want to "start" a new fragment using replace. This is the result. I was supposed to get only the BUTTON:
This is inside onItemClick:
Fragment nextFrag= new VitaminFragment();
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
transaction.replace(vitamin_fragment.getId(), nextFrag);
transaction.addToBackStack(null);
transaction.commit();
Inside VitaminFragment's 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);
}
and 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's layout is created programatically:
@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;
}
Finally HERE is the Activity that fragments are attached.
android android-layout android-fragments|
this question edited Feb 24 '15 at 1:33 asked Feb 24 '15 at 0:33 Manos 837 12 28
|
1 Answers
1
解决方法
The replace() method from FragmentTransaction simply removes all fragment from the specified container and then call add(). So if the specified container is empty, replace() simply adds a fragment on top of the current view.
推荐:android Fragments详解二:创建Fragment .
http://blog.csdn.net/nkmnkm/article/details/7169323 创建Fragment 要创建fragment,必须从Fragment或Fragment的派生类派生出一个类。Fragment的代码写起
You should post the XML file for your VitaminFragment, but I'd say add a background to your VitaminFragment and you should be fine.
And don't forget to make the main layout of your fragment clickable. Otherwise any click on something else than the button will go through the Fragment and probably click on an item on you ListView.
Your Fragment should look something like this :
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true" >
EDIT : If you want the replace() method to work and actually remove the ListView from the activity, make sure you call it on the same container that you used to add the first fragment in the first place. Also, the first Fragment needs to have been added dynamically. When I say added dynamically, I mean using the FragmentTransaction.add() method.
|
this answer edited Feb 24 '15 at 2:25 answered Feb 24 '15 at 1:20 JDenais 1,181 6 20 VitaminFragment has no XML. It is created programatically... – Manos Feb 24 '15 at 1:22 I don't get it. Which one is the new Fragment that you want to add on top of the ListView ? Cause the code you submitted seems to be for a fragment with a ListView in it and I don't see any Button. So I'm not sure i'm looking at the right thing. – JDenais Feb 24 '15 at 1:31 VitaminFragment i want to add on top of the list view. Check my edit – Manos Feb 24 '15 at 1:34 Ok so my answer stands. Change the XML file you just posted (for the supplement_property_layout) adding a background of your choice and making it clickable. – JDenais Feb 24 '15 at 1:38 What if i want to keep activity's background? any idea? – Manos Feb 24 '15 at 1:47 | show more comments
推荐:android Fragments详解四:管理fragment
要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情: 1使用方法findFra
推荐:android Fragments详解四:管理fragment
要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情: 1使用方法findFra
相关阅读排行
- 1Android Https相关完全解析 当OkHttp遇到Https
- 2Android APK反编译详解(附图)
- 3[置顶] Android APK反编译就这么简单 详解(附图)
- 4Android Fragment 真正的完全解析(上)
- 5Android RecyclerView 使用完全解析 体验艺术般的控件