如何在Android的不同布局交换片段? [英] How to swap fragments with different layouts in android?

查看:174
本文介绍了如何在Android的不同布局交换片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序利用碎片这更是一个主/详细信息的形式。我想主要活动包括在左侧的列表片段的基础上,选择对我想显示与在右侧不同布局的片段左侧的项目。
(注:在右侧的每个片段需要不同的布局/视图)

I'm developing an android application making use of fragments which is more of a Master/Detail form. I want the main activity to consist of a list fragment on the left side, based on the item chosen on the left i want to display a fragment with different layout on the right side. (Note: each fragment on the right require different layouts/views)

这是我在它改变一些价值观或交换/替换具有相同布局的新片段遇到化妆只用一个右共同片段的例子。

All the examples that I've come across make use of only one common fragment on the right by changing some values in it or swapping/replacing new fragments having same layout.

如果有人能在这个问题上提供一些线索,然后它会帮助我极大。谢谢!

If someone could shed some light on this problem then it will help me immensely. Thanks!

推荐答案

如果您使用的是framelayouts握住你的片段,这是一样的其他你提到。你只需实例化片段(无论布局),并将其交换到的FrameLayout代替另一种的。

If you are using framelayouts to hold your fragments, it's the same as those other you mention. You just instantiate your fragment (whatever the layout) and swap it into the framelayout in place of the other one.

如果您有硬codeD在碎片进入XML,你将不能够做到这一点(据我已经能够确定)。

If you have hardcoded your fragments into the XML, you won't be able to do that (as far as I've been able to determine).

的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frames"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/hline1"
    android:layout_below="@id/horizontalline"
    android:orientation="horizontal" >
    <FrameLayout
        android:id="@+id/leftpane"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight=".4" />
    <TextView
        android:id="@+id/verticalline"
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="@color/bar_background"
        android:gravity="center_horizontal"
        android:paddingLeft="5dip"
        android:paddingRight="5dip" />
    <FrameLayout
        android:id="@+id/rightpane"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>

然后你使用了的FrameLayout和你实例化片段的名称id来把你的片段到的FrameLayout。

Then you use the id for the framelayout and the name of your instantiated fragment to put your fragment into the framelayout.

EventListFragment eventlist = new EventListFragment();
getFragmentManager().beginTransaction().replace(R.id.leftpane, eventlist).commit();

EventDetailFragment eventadd = new EventDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventadd).commit();

当你想改变的内容,再次做同样的事情(以下将用新的/不同的片段,它可以有它与它相关的,不同的布局替换片段在右窗格中):

When you want to change the contents, you do the same thing again (the following would replace the fragment in the right pane with a new/different fragment, which can have it's own, different, layout associated with it):

EventSuperDetailFragment eventsuper = new EventSuperDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventsuper).commit();

这篇关于如何在Android的不同布局交换片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆