【问题标题】:Fragment application displaying 2 instances of the same fragment in Android Studio在 Android Studio 中显示相同片段的 2 个实例的片段应用程序
【发布时间】:2014-11-19 23:28:42
【问题描述】:

我正在编写一个在主要活动中显示片段的简单应用程序。当我运行应用程序时,它会在屏幕上显示片段的两个实例。我已经包含了下面的代码。顺便说一句,当我从主活动类中删除FT.commit(); 时,屏幕上只会显示一个片段实例。这里有什么问题?

谢谢。

片段布局 XML 文件

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

片段类

    package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragmentone extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragoner, container, false);
    }
}

主要活动布局 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<fragment
    android:name="com.example.fragment.fragmentone"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragone"
    tools:layout="@layout/fragoner">


    </fragment>

</RelativeLayout>

主要活动类

    package com.example.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager manager = getFragmentManager();
        FragmentTransaction FT = manager.beginTransaction();

        fragmentone Fone = new fragmentone();

        FT.add(R.id.fragone,Fone);
        FT.commit();


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

    标签: java android android-layout android-fragments android-studio


    【解决方案1】:

    这里有什么问题?

    如果您使用&lt;fragment&gt; 标记,请不要使用FragmentTransaction 添加相同的片段。这是两种不同的技术,不应混用。

    只需删除来自onCreate() 的每一行,除了对super.onCreate()setContentView() 的调用,并坚持使用静态&lt;fragment&gt;

    【讨论】:

    • 你能解释一下为什么不应该混合使用&lt;fragment&gt;标签和FragmentTransaction吗?
    • @David: &lt;fragment&gt; 用于静态片段,它们将永远存在。问题中的代码最终将创建两个重叠的fragmentone 实例:一个来自&lt;fragment&gt;,另一个来自FragmentTransaction。在基于 Jetpack 的 Android 应用开发中,您将 &lt;fragment&gt; 用作您的 NavHostFragment,并让它处理显示所有其他片段的工作。
    【解决方案2】:

    要么从活动的布局视图中删除,要么停止使用提交。您可以使用 a 作为片段的容器。将 id 添加到您的 FrameLayout 而不是使用: FragmentManger manager=getFragmentManager(); Fragment fragment=manager.findFragmentById(R.id.frameContainer);

    【讨论】:

      猜你喜欢
      • 2017-09-30
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多