android - 如何在选项卡式应用程序中替换 LinearLayout 下的 fragment

标签 android android-layout android-fragments android-linearlayout android-tabs

我有一个带有 3 个 xml 布局文件的选项卡式应用程序。 fragment 以编程方式创建并添加到 LinearLayouts。

但是,其中一个 LinearLayouts 看起来像这样:

map .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/primary"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="YOLO" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/secondary"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

此选项卡应分为两种布局,一种占屏幕的 25%,另一种占 75%。

这使我的 TabListener 无法替换整个 .xml 文件,而只能替换 secondary LinearLayout,因为这是我在 onCreate 方法期间添加 Fragment 的地方的 Activity 。

因此,我最终将应用程序顶部的 25% 设为静态且永不更改。

当我尝试替换父布局 map 时,应用程序崩溃了。

我能做什么?

package com.nfc.demo;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;

public class NFCDemoActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        GoogleMapOptions options = new GoogleMapOptions();
        options.mapType(GoogleMap.MAP_TYPE_SATELLITE).compassEnabled(false)
                .rotateGesturesEnabled(false).tiltGesturesEnabled(false);

        MapFragment mMapFragment = MapFragment.newInstance(options);
        Fragment mSettingsFragment = new SettingsFragment();
        Fragment mAboutFragment = new AboutFragment();

        ft.add(R.id.secondary, mMapFragment);
        ft.commit();

        ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        ActionBar.Tab mapTab = bar.newTab().setText("Map");
        ActionBar.Tab settingsTab = bar.newTab().setText("Settings");
        ActionBar.Tab aboutTab = bar.newTab().setText("About");

        mapTab.setTabListener(new TabListener(mMapFragment));
        settingsTab.setTabListener(new TabListener(mSettingsFragment));
        aboutTab.setTabListener(new TabListener(mAboutFragment));

        bar.addTab(mapTab, 0);
        bar.addTab(settingsTab, 1);
        bar.addTab(aboutTab, 2);

        setContentView(R.layout.map);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    protected class TabListener implements ActionBar.TabListener {
        private Fragment fragment;

        public TabListener(Fragment fragment) {
            this.fragment = fragment;
        }

        public void onTabSelected(Tab tab,
                FragmentTransaction fragmentTransaction) {
            fragmentTransaction.replace(R.id.secondary, this.fragment, null);
        }

        public void onTabUnselected(Tab tab,
                FragmentTransaction fragmentTransaction) {
            fragmentTransaction.remove(this.fragment);
        }

        public void onTabReselected(Tab tab,
                FragmentTransaction fragmentTransaction) {
            // do nothing
        }
    }
}

尝试替换 R.id.map 而不是替换 R.id.secondary 时的错误日志

12-30 14:01:16.035: E/AndroidRuntime(12377): FATAL EXCEPTION: main
12-30 14:01:16.035: E/AndroidRuntime(12377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nfc.demo/com.nfc.demo.NFCDemoActivity}: java.lang.IllegalStateException: Can't change container ID of fragment MapFragment{41ca1660 id=0x7f04000c}: was 2130968588 now 2130968585
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.os.Looper.loop(Looper.java:137)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at java.lang.reflect.Method.invokeNative(Native Method)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at java.lang.reflect.Method.invoke(Method.java:511)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at dalvik.system.NativeStart.main(Native Method)
12-30 14:01:16.035: E/AndroidRuntime(12377): Caused by: java.lang.IllegalStateException: Can't change container ID of fragment MapFragment{41ca1660 id=0x7f04000c}: was 2130968588 now 2130968585
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.BackStackRecord.doAddOp(BackStackRecord.java:407)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.BackStackRecord.replace(BackStackRecord.java:429)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.nfc.demo.NFCDemoActivity$TabListener.onTabSelected(NFCDemoActivity.java:77)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:509)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:490)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at com.nfc.demo.NFCDemoActivity.onCreate(NFCDemoActivity.java:47)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.Activity.performCreate(Activity.java:5104)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-30 14:01:16.035: E/AndroidRuntime(12377):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-30 14:01:16.035: E/AndroidRuntime(12377):    ... 11 more

最佳答案

好的,我查看了 BackStackRecord 的源代码,发现 fragment 实例只能附加到一个容器的 id。

所以这个问题的解决方案是以编程方式创建FrameLayout。给它设置一些id。将创建的布局添加到第一个 LinearLayout。将 fragment 添加到 FrameLayout 的 id。并在需要时从一个 LinearLayout 中删除创建的布局并添加到另一个。

示例代码

public void onCreate(Bundle savedInstanceState) {
    ...

    mLayout = new FrameLayout(this);
    mLayout.setId(R.id.tertiary);
    ((LinearLayout) findViewById(R.id.primary)).addView(mLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
    ft.add(R.id.tertiary, mMapFragment);
    ft.commit();

    ...
}

...

    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        ((LinearLayout) findViewById(R.id.primary)).removeView(mLayout);
        ((LinearLayout) findViewById(R.id.secondary)).addView(mLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
    }

关于android - 如何在选项卡式应用程序中替换 LinearLayout 下的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090751/

相关文章:

android - 如何以编程方式删除在 XML 中定义的现有规则?

java - 从另一个 fragment 调用一个 fragment ,然后替换其布局

java - viewPager 上的 RecyclerView 崩溃

android - Android中是否有用于谷歌地图导航的API?

android - 将 Fabric 的 Crashlystic 数据迁移到 Firebase

android - 强制 android 使用方形启动器图标

Android - 在应用程序启动时加载 Unity3d 模型/ View ,但在不同的 Activity 中显示

android - android抽屉布局上的侧边栏导航标题

android - 将图像定位在形状的右下角

android - 在 fragment 中使用资源