片段不会出现onButtonClick

时间:2017-08-17 13:38:08

标签: android android-fragments

片段默认显示,但我希望它隐藏/隐藏,直到我点击此按钮。我遇到的问题是,当我单击此按钮时,片段将不会显示。谈到碎片,我是一个完全的初学者。

我的主要活动中的代码:

package com.example.julian.tutorial_fragment;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View frag = findViewById(R.id.fragment3);
        frag.setVisibility(View.GONE);
    }

    //This gets called by the TopFragment when the user clicks the button
    @Override
    public void createMeme(String top, String bottom) {
        BottomPictureFragment bottomFragment = (BottomPictureFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
        bottomFragment.setMemeText(top, bottom);
    }
//this is the onButtonClick
    public void sample(View view) {
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment_Search hello = new Fragment_Search();
        fragmentTransaction.add(R.id.fragment3, hello, "HELLO");
        fragmentTransaction.commit();
    }
}

我在Fragment_Search中的代码:

package com.example.julian.tutorial_fragment;

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

/**
 * Created by Julian on 8/17/2017.
 */

public class Fragment_Search extends Fragment {

    public interface Fragment_SearchInterface {


    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sample_layout, container, false);

        return view;
    }
}

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.julian.tutorial_fragment.MainActivity">

    <RelativeLayout
        android:layout_width="377dp"
        android:layout_height="498dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <fragment
            android:id="@+id/fragment2"
            android:name="com.example.julian.tutorial_fragment.BottomPictureFragment"
            android:layout_width="250dp"
            android:layout_height="250dp"
            tools:layout="@layout/bottom_picture_fragment"
            tools:layout_editor_absoluteX="67dp"
            tools:layout_editor_absoluteY="223dp"
            android:layout_marginBottom="30dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

        <fragment
            android:id="@+id/fragment"
            android:name="com.example.julian.tutorial_fragment.TopSectionFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:layout="@layout/top_section_fragment"
            tools:layout_editor_absoluteX="42dp"
            tools:layout_editor_absoluteY="16dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/fragment2"
            android:layout_alignStart="@+id/fragment"
            android:text="Button"
            android:onClick="sample"/>

        <fragment
            android:id="@+id/fragment3"
            android:name="com.example.julian.tutorial_fragment.Fragment_Search"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_below="@+id/button2"
            android:layout_marginTop="92dp"
            android:layout_toStartOf="@+id/button2" />
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

更改您的示例功能并添加此

FragmentManager fragmentManager = getSupportFragmentManager();

    fragmentManager.beginTransaction()
            .replace(R.id.your_layout, new Fragment_Search())
            .addToBackStack(null)
            .commit();