如何更换不同类型的片段? [英] How to replace Fragments of different types?

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

问题描述

早上好。我开发一个应用程序中,我有一个从活动,这其中内延伸一个主要布局我有一个片段一个数据类型,在我的情况FragmentCover(这是一个类)。

Good morning. I'm developing an app in which I have a main layout that extends from activity and inside this one I have one fragment of one data type, in my case FragmentCover (it's a class).

在我的应用程序,我按下按钮,我想改变这个片段布局另一个布局,从片段,但不同类型的扩展,叫的SongList。

During my app, I push a button and I want to change this fragment layout for another layout that extends from fragment but of different type, called SongList.

我的问题是,我已经定义了这个片段的类封面,当我改变我没有任何问题,但是当我想要得到的意见,并设置为我的类的一个变量,则funciont 的SongList =(ListView控件)getView()findViewById(R.id.songList); 它为空,这让我误差

My problem is that I have defined this fragment for the class of Cover and when I change I don't have any problem, but when I want to get the views and set to one variable of my class, the funciont songList = (ListView) getView().findViewById(R.id.songList); it's null and it gives me error.

我把它放在这里我做什么。

I put it here what I do.

布局

<fragment android:name="es.xxx.ui.FragmentCover"
    android:id="@+id/pruebaa"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/songBar"
    android:layout_below="@id/header"/>

mainclass更改

public void onClick(View v) {
if(isCoverFragment){
                FragmentSongList fragmentSongList = new FragmentSongList();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pruebaa, fragmentSongList);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.addToBackStack("LIST");
                transaction.commit();
                isCoverFragment=false;
            }
            else{
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pruebaa, fragmentCover);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.addToBackStack("COVER");
                transaction.commit();
                getSongCover();
                isCoverFragment=true;
            }

问题不改变,是当我试着让findViewById,这是probabbly,因为它不会加载与FragmentSongList相关的看法。

The problem is not the change, is when I try to make the findViewById, it's probabbly because it doesn't load the view associated with FragmentSongList.

推荐答案

您需要使用您的布局文件内的FrameLayout,而不是在定义片段的.xml

成的FrameLayout,可以膨胀任何你想要的片段。
充气第一个片段,你要在你的的onCreate(...)方法直接显示。

Into that FrameLayout, you can inflate any Fragment you want. Just inflate the First Fragment you want to be displayed directly in your onCreate(...) method.

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
        </FrameLayout>

这是怎样的膨胀的碎片编程到的FrameLayout。

This is how to inflate the Fragments programatically into the FrameLayout.

 FragmentSongList fragmentSongList = new FragmentSongList();
 FragmentTransaction transaction = getFragmentManager().beginTransaction();
         transaction.replace(R.id.content_frame, fragmentSongList);
         transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         transaction.addToBackStack("LIST");
         transaction.commit();

您可以离开code你的onClick内()方法只是事情是这样的,刚刚的更改ID为content_frame。此外,如前所述以上你将不得不充气应您的onCreate(...)方法中显示的第一个片段。

You can leave the code inside your onClick() method just the way it is, just change the id to "content_frame". Furthermore, as mentioned above you will have to inflate the first Fragment that should be displayed inside your onCreate(...) method.

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

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