简体   繁体   English

活动中重叠的片段

[英]Overlapping fragments in activity

I have been learning android app development for a couple of days using the 'developer.android.com' tutorial. 我已经使用“ developer.android.com”教程学习了android应用开发了几天。 Currently I m learning the concept of fragments and frame-sets. 目前,我正在学习片段和框架集的概念。 I have written a hello world program using fragments. 我已经使用片段编写了一个hello world程序。 But the prob is that my fragment layout gets inserted in the fragment container twice and the result is 2 overlapped fragments in the frame-set. 但问题是我的片段布局两次插入片段容器中,结果是框架集中有2个重叠的片段。 Can u please tell me why this happens and what is wrong with my code? 您能告诉我为什么会这样吗,我的代码有什么问题吗?

Below is my Home_page.java (the activity which contains the fragment_container) 以下是我的Home_page.java(包含fragment_container的活动)

package com.technology.computer.mit.ctechmit;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class Home_page extends ActionBarActivity {

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



        // Create a new Fragment to be placed in the activity layout
        Home_pageFragment firstFragment = new Home_pageFragment();
        // 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();


    }


    @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_home_page, 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);
    }
}

Below is my Home_pageFragment.java (the fragment which is supposed to be inserted in the container) 下面是我的Home_pageFragment.java(应该插入容器的片段)

package com.technology.computer.mit.ctechmit;

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


/**
 * A placeholder fragment containing a simple view.
 */
public class Home_pageFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home_page, container, false);
    }

}

Below is my activity_home_page.xml (fragment_container layout) 以下是我的activity_home_page.xml(fragment_container布局)

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:name="com.technology.computer.mit.ctechmit.Home_pageFragment"
    tools:layout="@layout/fragment_home_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Below is my fragment_home_page.xml (fragment layout) 以下是我的fragment_home_page.xml(片段布局)

<LinearLayout
    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"
    android:orientation="horizontal"
    tools:context=".Home_pageFragment">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />

</LinearLayout>

Can u please help me with this error? 您能帮我解决这个错误吗? Why is the fragment getting loaded twice in the home activity? 为什么片段在家庭活动中被加载两次? I am getting an overlapped hello world text. 我收到了重叠的Hello World文字。

It's simple. 这很简单。 The layout you are inserting your Fragment is not a view that you are using as a Container. 您要插入片段的布局不是用作容器的视图。 In fact, it's fragment itself. 实际上,它本身就是碎片。

So either use a FrameLayout as a fragment container, or keep the Fragment in your XML and do not call Fragment Manager to insert to this Fragment. 因此,要么将FrameLayout用作片段容器,要么将Fragment保留在XML中,并且不要调用Fragment Manager插入此Fragment。 I would suggest the first one. 我建议第一个。

<FrameLayout
   android:id="@+id/fragment_container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM