按钮点击更改片段

时间:2015-04-02 13:53:01

标签: java android android-fragments android-activity fragment

我必须创建一种视图,其中包含由"下一个按钮"选择的少数片段。我有一个名为FragmentSearch的片段,其中包含一个按钮,当我点击按钮时,我想转到下一个片段,FragmentBoxOffice,但我不知道如何做到这一点。 我尝试在按钮的setOnClickListener中使用inflater.inflate,但这似乎不起作用。此外,我尝试在单个片段中创建所有内容,使用setVisibity对视图进行某种更改,但这也无法正常工作。

public class FragmentSearch extends Fragment implements SortListener{

...
@Override
public View onCreateView(  LayoutInflater inflater,   ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(fragment_search, container, false);

    Button but =(Button) view.findViewById(R.id.button);

    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        myView = inflater.inflate(fragment_box_office, container, false);

        }
    });
    return inflater.inflate(R.layout.fragment_search, container, false);
}

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.altervista.ilpixelmatto.superfilm.Fragment.FragmentSearch">

<RelativeLayout
    android:id="@+id/frame1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible">


<TextView
        android:id="@+id/text11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="frame1"
        android:paddingLeft="50dp"
        android:textColor="@color/Black" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal" />

    <Button
        android:id="@+id/button"
        android:layout_below="@id/text11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="frame1" />
</RelativeLayout>



<!-- TODO: Update blank fragment layout -->


</LinearLayout>

怎么做?

1 个答案:

答案 0 :(得分:2)

显示片段的唯一正确方法是使用FragmentManagerFragmentTransaction。将onClick()替换为:

public void onClick(View v) {
    FragmentManager fm = getFragmentManager();  
    FragmentBoxOffice f = (FragmentBoxOffice) fm.findFragmentByTag(FragmentBoxOffice.TAG);
    if (f == null) {
      f = new FragmentBoxOffice();
      fm.beginTransaction()
        .replace(R.id.fragment_container, f, FragmentBoxOffice.TAG)
        //.addToBackStack(null);  // uncomment this line if you want to be able to return to the prev. fragment with "back" button
        .commit();
    }
}

然后在您的活动布局中,您需要一个名为ViewGroup的占位符fragment_container,它将成为片段布局的父级:

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

两个片段都应该有自己的布局,这些布局放在活动布局的fragment_container中。