2017-08-17 68 views
-1

片段默認顯示,但我希望它被隱藏/隱藏,直到我點擊這個按鈕。我遇到的問題是,當我點擊這個按鈕時,片段不會顯示出來。當涉及到碎片時,我是一個完整的初學者。片段不會顯示onButtonClick

我在我的主要活動代碼:

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

顯示您的activity_main.xml –

+0

除了使用平板電腦時,您不得不在同一佈局中同時使用3(!)片段。我認爲你應該重新考慮你的佈局,只使用一個片段,在必要時你會用另一個片段替換。另外你不需要'ConstraintLayout'和'RelativeLayout',因爲RelativeLayout在ContstraintLayout中是獨立的 – Eselfar

+0

另外兩個我從教程中得到它,我用它來測試它是否讀取我輸入並放置的數據它在第二個片段的文本視圖中。當我知道它的工作。我想嘗試使用僅通過單擊此按鈕顯示的片段,它將顯示在主要活動的上方。然而,我看不到任何教程顯示這樣的片段。我看到一個,但它使用SupportMapFragment,找不到其他人有類似的教程。 –

回答

0

改變你的樣品的功能和添加此

FragmentManager fragmentManager = getSupportFragmentManager(); 

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

當我單擊示例按鈕時,它仍然不會顯示 –

+0

從xml中刪除第3個片段並添加FrameLayout並傳遞framelayout的id以替換 – nKalai

+0

我刪除了第3個片段並添加了包含此代碼的框架佈局: 單擊按鈕時它仍然不顯示藍色背景。 –