2016-08-04 55 views
0

Jungs ich verliere meinen Verstand, ich möchte meine Haupttätigkeit, um die Benutzeroberfläche mit dem Button anzuzeigen, sobald ich auf den Button ich möchte das Fragment zu öffnen, aber ich kann nicht scheinen, es richtig zu machen Ich weiß, dass ich einen ViewGroup-Container erstellen muss, damit ich die Transaction.add-Methode verwenden kann, aber ich habe keine Ahnung, was ich als nächstes tun soll oder wo ich es hinstellen soll. Bitte helfen Sie, Vielen Dank im Voraus.Instanziieren Fragment von Aktivität über OnClickListner

Main.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 


public class MainActivity extends AppCompatActivity { 

    Button b1; 

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

     b1=(Button)findViewById(R.id.btnFragmentA); 

     b1.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       Fragment newFragment = new Fragment(); 
       FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

       transaction.add(R.id.fragment1 , newFragment); 
       transaction.commit(); 
      } 
     }); 
    } 
} 

Fragment

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


public class FragmentA extends Fragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     return inflater.inflate(R.layout.a_fragment, container, false); 
    } 
} 

Haupt XML

<?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: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" 
    tools:context="com.dane.fragmenttest.MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Fragment A" 
     android:id="@+id/btnFragmentA" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

Fragment 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" 
    android:weightSum="0"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is the fragment" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

Antwort

0

Nun, ich denke, dass Sie nur zwei Dinge tun müssen.

  1. Fügen Sie einen Container zu Ihrem Haupt-XML hinzu, um FragmentA zu speichern.

Zum Beispiel:

<?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: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" 
    tools:context="com.dane.fragmenttest.MainActivity"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/button" /> 

    <Button 
     android:id="@+id/btnFragmentA" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Fragment A" /> 

</RelativeLayout> 
  1. aktualisieren onClickListener() auf MainActivity

Zum Beispiel:

b1.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     FragmentA newFragment = new FragmentA(); 

     getFragmentManager().beginTransaction() 
      .add(R.id.container , newFragment) 
      .commit(); 
    } 
}); 

Ich hoffe, das hilft.