在片段视图之间切换

发布于 2024-12-10 04:55:24 字数 406 浏览 1 评论 0原文

在 xml 布局文件中声明片段的标准方法是

<LinearLayout ...> 
    <fragment class="com.example.SomeFragment"
</LinearLayout>

SomeFragment 是一个 java 类,定义如下:

class SomeFragment extends Fragment { 
    ... 
}

让我们说,我有 3 个片段;片段 1、片段 2 和片段 3。当用户启动应用程序时,我向他们显示fragment1,当他们单击按钮时,我将fragment1 替换为fragment2,等等。

在单个布局xml 文件中定义3 个片段的最佳方法是什么?

The standard way to declare fragments in a xml layout file is

<LinearLayout ...> 
    <fragment class="com.example.SomeFragment"
</LinearLayout>

where SomeFragment is a java class defined like

class SomeFragment extends Fragment { 
    ... 
}

Lets say, I have 3 fragments; fragment1, fragment2, and fragment3. When the user launches the app, I show them fragment1, and when they click on a button, I replace the fragment1 with fragment2, etc.

What is the best approach to define the 3 fragments in a single layout xml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-12-17 04:55:24

为此,您应该使用 FrameLayout,这样您就不必在 XML 中指定片段类,并且它不限于一个类。

<FrameLayout 
    android:id="@+id/contentFragment"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" />

然后你可以像这样在代码中设置片段

Fragment fragment = new YourFragment();

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.contentFragment, fragment);
transaction.commit();

You should use a FrameLayout for that, that way you don't have to specify the fragment class in the XML and that way it is not limited to one class.

<FrameLayout 
    android:id="@+id/contentFragment"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" />

and than you can set the fragment in the code like this

Fragment fragment = new YourFragment();

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.contentFragment, fragment);
transaction.commit();
甚是思念 2024-12-17 04:55:24

我给出了一个在片段中的两个布局之间切换的示例:

首先声明一个包含两个片段的布局:(这取决于您想要在布局中包含多少个片段)

fragment_layout_example.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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

    <fragment
        android:id="@+id/Fragment2"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.example.SecondFragment" >

        <!-- Preview: layout=@layout/details -->
    </fragment>
</LinearLayout>

上面的布局将显示两个片段 Fragment1 和 Fragment2。
对于 Fragment1,我已声明容器,因为容器的内容将在运行时更改。所以这里没有声明 Fragment 类。有关此检查的更多信息

http://developer.android.com/training/ basics/fragments/fragment-ui.html

然后创建一个继承 Activity 的 FragmentExampleActivity 类。如果您在向后兼容模式下使用 Fragment,则扩展 FragmentActivity

  public class FragmentExampleActivity extends FragmentActivity{

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_layout_example);

     // Check that the activity is using the layout version with
    // the fragment_container FrameLayout
    if (findViewById(R.id.fragment_container) != null) {

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create an instance of Fragment1
        Fragment1 firstFragment = new Fragment1();

        // In case this activity was started with special instructions from an Intent,
        // pass the Intent's extras to the fragment as arguments
        firstFragment.setArguments(getIntent().getExtras());

        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
  }

 }

要为两个片段创建布局,请创建两个扩展 Fragment 的类,

public class Fagment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //set the layout you want to display in First Fragment
    View view = inflater.inflate(R.layout.fragment1,
            container, false);
    return view;

}

}

以同样的方式为第二个 Fragment 创建 Fragment 类并设置 现在的布局

如果您想通过单击按钮将 Fragment1 中的片段布局切换到另一个布局,请创建另一个类(例如 Fragment3.java)并设置要切换的布局,并在Fragment1.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button showFragment3=(Button)getView().findViewById(R.id.Button1);
    showFragment3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();

            Fragment3 fragment3 = new Fragment3();
            fragmentTransaction.replace(R.id.Fragment1, fragment3);
//provide the fragment ID of your first fragment which you have given in
//fragment_layout_example.xml file in place of first argument
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });

}

现在要再次返回第一个片段,您可以单击后退按钮。但如果您想通过单击按钮返回,请在 Fragment3.java 中编写以下代码,

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button showFragment1 = (Button) getView().findViewById(
            R.id.Button2);
    showFragment1 .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            getFragmentManager().popBackStack();
        }
    });

}

谢谢!希望它能帮助你...

I am giving an example to switch between two layouts in a fragments:

First declare a layout with two fragments:(it depends on how many fragments you want in your layout)

fragment_layout_example.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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

    <fragment
        android:id="@+id/Fragment2"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.example.SecondFragment" >

        <!-- Preview: layout=@layout/details -->
    </fragment>
</LinearLayout>

Above layout will display two fragments Fragment1 and Fragment2.
For Fragment1 I had declared the container as content of the container is going to changed at runtime. So not declared the Fragment class here. for more on this check

http://developer.android.com/training/basics/fragments/fragment-ui.html

Then create a class FragmentExampleActivity which extends Activity. In case you are using Fragment in Backward compatibility mode then extend FragmentActivity

  public class FragmentExampleActivity extends FragmentActivity{

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_layout_example);

     // Check that the activity is using the layout version with
    // the fragment_container FrameLayout
    if (findViewById(R.id.fragment_container) != null) {

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create an instance of Fragment1
        Fragment1 firstFragment = new Fragment1();

        // In case this activity was started with special instructions from an Intent,
        // pass the Intent's extras to the fragment as arguments
        firstFragment.setArguments(getIntent().getExtras());

        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
  }

 }

To create a layout for two fragments create two classes which extends Fragment

public class Fagment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //set the layout you want to display in First Fragment
    View view = inflater.inflate(R.layout.fragment1,
            container, false);
    return view;

}

}

Same way create Fragment class for second Fragment and set the layout

Now If you want to switch your fragment layout in Fragment1 to another layout on click of a button then create another class say Fragment3.java and set the layout you want to switch and write the below code inside the Fragment1.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button showFragment3=(Button)getView().findViewById(R.id.Button1);
    showFragment3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();

            Fragment3 fragment3 = new Fragment3();
            fragmentTransaction.replace(R.id.Fragment1, fragment3);
//provide the fragment ID of your first fragment which you have given in
//fragment_layout_example.xml file in place of first argument
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });

}

Now to come back again on the first Fragment you can click on back button. But if you want to come back on click of button then write the below code in Fragment3.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button showFragment1 = (Button) getView().findViewById(
            R.id.Button2);
    showFragment1 .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            getFragmentManager().popBackStack();
        }
    });

}

Thanks! Hope it will help you...

旧伤慢歌 2024-12-17 04:55:24

在我的片段中,我创建了一个方法

public void switchFragment(Fragment fragment){
   FragmentManager fm = getFragmentManager();
    fm.beginTransaction()
            .replace(R.id.fr_layout_merchant, fragment)
            .addToBackStack(null)
            .commit();
}

然后在任何需要在执行进程后切换片段的例程中我做

switchFragment(new TheFragmentIWantToSwitchTo)

In my fragment, I had created a method

public void switchFragment(Fragment fragment){
   FragmentManager fm = getFragmentManager();
    fm.beginTransaction()
            .replace(R.id.fr_layout_merchant, fragment)
            .addToBackStack(null)
            .commit();
}

And then in any of the routine that needs a switch of fragment after carrying a process i do

switchFragment(new TheFragmentIWantToSwitchTo)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文