2015-02-24 108 views
0

我在片段中有一个ListView。当我点击列表上的一个项目时,我想使用替换“开始”新的片段。这是结果。我应该只得到BUTTON从片段中替换片段导致片段内容重叠

enter image description here

这里面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); 
} 

和Vitami nFragment 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是片段所连接的活性的影响。

回答

8

FragmentTransaction的replace()方法只是从指定容器中删除所有片段,然后调用add()。所以如果指定的容器是空的,replace()只是在当前视图的顶部添加一个片段。

你应该发布你的维生素片段的XML文件,但我会说增加一个背景到你的维生素片段,你应该没问题。

并且不要忘记让你的片段的主要布局可点击。否则,除按钮以外的其他任何点击都将通过片段,并可能点击ListView上的某个项目。

你的片段应该是这个样子:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:clickable="true" > 

编辑:如果你想要的replace()方法的工作,实际上从活动中删除的ListView,确保你叫它同一容器上你曾经在第一个地方添加第一个片段。另外,第一个片段需要动态添加。当我说动态添加时,我的意思是使用FragmentTransaction.add()方法。

+0

VitaminFragment没有XML。它以编程方式创建... – Manos 2015-02-24 01:22:16

+0

我不明白。哪一个是你想添加在ListView之上的新片段?导致您提交的代码似乎是针对带有ListView的片段,并且我没有看到任何Button。所以我不知道我在看正确的事情。 – JDenais 2015-02-24 01:31:46

+0

我想添加在列表视图顶部的VitaminFragment。检查我的编辑 – Manos 2015-02-24 01:34:47