简体   繁体   English

Android onClick on Main 打开fragment

[英]Android onClick on Main to open fragment

I'm having problems using fragments.我在使用片段时遇到问题。 I want to open a fragment programatically on MainActivity when I press a button.当我按下按钮时,我想在 MainActivity 上以编程方式打开一个片段。 With the code below the app runs but it don't opens the fragment:使用下面的代码运行应用程序,但它不会打开片段:

MyFragment fragment = new MyFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container,fragment, "Hello");
transaction.commit();

What I'm doing wrong here?我在这里做错了什么? I've done a lot of research on stack overflow but I can't see a question that suits my needs.我对堆栈溢出做了很多研究,但找不到适合我需求的问题。

EDIT编辑

This is the layout where I want the fragment to appear:这是我希望片段出现的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/content1"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_marginTop="96dp"
    tools:context="com.example.sdilab.pap.MainActivity">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="2">

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@id/cardView"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"

            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/exercicio"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:id="@+id/exerciciotext"
                android:textAlignment="center"
                android:gravity="bottom"
                android:layout_marginBottom="36dp"
                android:text="Registar\nexercício"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@id/cardView"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"

            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/food"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textAlignment="center"
                android:gravity="bottom"
                android:id="@+id/foodtext"
                android:layout_marginBottom="36dp"
                android:text="Registar\nrefeição"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="2">

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/cardViewCalendar"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:elevation="0dp"
            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView3"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/calendar"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textAlignment="center"
                android:id="@+id/calendartext"
                android:gravity="bottom"
                android:layout_marginBottom="36dp"
                android:text="Agendar\ntreinos"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@id/cardView"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"

            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView4"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/settings"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textAlignment="center"
                android:id="@+id/settingstext"
                android:gravity="bottom"
                android:layout_marginBottom="36dp"
                android:text="Alterar\ndefinições"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>
    </LinearLayout>

</LinearLayout>

Replace代替

transaction.add(R.id.fragment_container,fragment, "Hello");

With

transaction.add(R.id.fragment_container,fragment);

Try with this试试这个

 FragmentManager mFragmentManager = getSupportFragmentManager();
            FragmentTransaction mFragmentTransaction =  mFragmentManager.beginTransaction();
            Fragment fragment = new MyFragment();
            mFragmentTransaction.add(R.id.fragment_container, fragment);
            mFragmentTransaction.commit();

Try to do a explicit call of show for your fragment after adding it.尝试做的显式调用show添加它后您的片段。 Something like this:像这样的东西:

FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction =  mFragmentManager.beginTransaction();
Fragment fragment = new MyFragment();
mFragmentTransaction.add(R.id.fragment_container, fragment);
mFragmentTransaction.show(fragment);
mFragmentTransaction.commit();

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

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