通过按钮点击更改片段

3
我需要创建一种视图,它由几个片段组成,这些片段是通过“下一个按钮”选择的。我有一个名为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>

如何做到这一点?


你想使用同一个按钮在不同的碎片之间切换吗? - Pankaj Nimgade
不,每个片段都有一个按钮,用于导航到下一个或上一个片段。 - IlPixelMatto
问题在于该片段仍处于可滚动选项卡中。我无法在另一个可滚动选项卡内添加第二个可滚动选项卡。我必须制作一种问卷,用户在此片段中回答一些问题,然后滚动到可滚动选项卡中的另一个片段。 - IlPixelMatto
是的,我知道。这确实是问题!XD 我必须使用此按钮在可滚动选项卡中切换片段,以避免使用另一个可滚动选项卡 :) - IlPixelMatto
在你的片段中创建一个根线性布局,其中包含按钮和帧布局,并使用该常量按钮更改帧布局的内容以使用不同的片段 :) - Pankaj Nimgade
显示剩余2条评论
1个回答

3

展示一个片段的唯一正确方法是使用 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();
    }
}

在您的活动布局中,您需要一个名为fragment_container的占位符ViewGroup,它将成为片段布局的父级:
<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

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

FragmentBoxOffice中的.TAG属性是什么? - IlPixelMatto
每个Fragment都会在FragmentManager中保留一个任意的字符串作为映射键。你可以在findFragmentByTag()方法中使用这个标签进行搜索。 附注:每个Fragment的标签值必须不同。 - WindRider
好的,我尝试了一下,但问题是“FragmentTransaction ft”必须是一个整数值,但它仍然不起作用...当我点击按钮时,似乎什么都没有发生 >.< 谢谢帮助...我已经尝试了3个小时 xD - IlPixelMatto
抱歉,我盲目地编写了代码。只需忽略提交的返回值即可,您不需要它。我已经修复了我的回答。 - WindRider
好的,问题在于该片段无法识别按钮点击。我尝试将 onClickListener 添加到按钮或片段中,但片段无法识别该事件。 - IlPixelMatto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接