2015-09-21 14 views
8

Ho qualche pulsante sul mio HomeFragment voglio aprire un frammento sul pulsante clic da un frammento. Qualcosa come la navigazione. Sto provando con il codice muggito ma non ha funzionato. Per favore aiuto ! Sono molto nuovo ad Android e sto cercando di imparare cose nuove.Come aprire un frammento sul pulsante clic da un frammento in Android

Ecco il mio codice

import android.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

public class HomeFragment extends Fragment { 

    public HomeFragment(){} 



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

     View rootView = inflater.inflate(R.layout.fragment_home, container, false); 

     Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton); 
     Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton); 
     Button schemeBtn = (Button) rootView.findViewById(R.id.schemeButton); 
     Button loanBtn = (Button) rootView.findViewById(R.id.loanButton); 
     Button serviceBtn = (Button) rootView.findViewById(R.id.serviceButton); 
     Button devBtn = (Button) rootView.findViewById(R.id.devButton); 

     aboutBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       // Launching new Activity on selecting single List Item 
       Intent i = new Intent(getActivity(), AboutFragment.class); 
       startActivity(i); 
      } 
     }); 

     phonebookBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       // Launching new Activity on selecting single List Item 
       Intent j = new Intent(getActivity(), PhoneBookFragment.class); 
       startActivity(j); 
      } 
     }); 

     schemeBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       // Launching new Activity on selecting single List Item 
       Intent k = new Intent(getActivity(), HomeFragment.class); 
       startActivity(k); 
      } 
     }); 

     loanBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       // Launching new Activity on selecting single List Item 
       Intent l = new Intent(getActivity(), RemittanceFragment.class); 
       startActivity(l); 
      } 
     }); 

     serviceBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       // Launching new Activity on selecting single List Item 
       Intent m = new Intent(getActivity(), ServiceFragment.class); 
       startActivity(m); 
      } 
     }); 

     devBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       // Launching new Activity on selecting single List Item 
       Intent n = new Intent(getActivity(), AboutDeveloper.class); 
       startActivity(n); 
      } 
     }); 


     return rootView; 
    } 
} 

Sto cercando qualcosa di simile codice soffietto, ma non so come fare il codice funziona con il mio codice.

Button aboutBtn = (Button) v.findViewById(R.id.aboutButton); 
aboutBtn.setOnClickListener(this); 

Button homeBtn = (Button) v.findViewById(R.id.homeButton); 
homeButton.setOnClickListener(this); 

Button serviceBtn = (Button) v.findViewById(R.id.serviceButton); 
serviceBtn.setOnClickListener(this); 

@Override 
public void onClick(View view) { 
    Fragment fragment = null; 
    switch (view.getId()) { 
     case aboutButton: 
      fragment = new AboutFragment(); 
      break; 

     case homeBtn: 
      fragment = new PhonebookFragment(); 
      break; 

     case serviceBtn: 
      fragment = new ServiceFragment(); 
      break; 

     default: 
      fragment = new HomeFragment(); 
      break; 

    } 
} 
+3

Non so perché la gente verso il basso voto su di esso? Se non puoi aiutare per favore non spezzarmi il cuore :( – Firefog

risposta

14

È possibile sostituire il frammento utilizzando FragmentTransaction al clic del pulsante. Qualcosa di simile a questo:

Fragment someFragment = new SomeFragment(); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.fragment_container, someFragment); // give your fragment container id in first parameter 
    transaction.addToBackStack(null); // if written, this transaction will be added to backstack 
    transaction.commit(); 

Informazioni sui performing fragment transactions here.

codice:

package com.rupomkhondaker.sonalibank; 

import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

public class HomeFragment extends Fragment implements View.OnClickListener { 

    public HomeFragment() { 
    } 

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

     View rootView = inflater.inflate(R.layout.fragment_home, container, false); 

     Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton); 
     Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton); 

     aboutBtn.setOnClickListener(this); 
     phonebookBtn.setOnClickListener(this); 


     return rootView; 
    } 

    @Override 
    public void onClick(View view) { 
     Fragment fragment = null; 
     switch (view.getId()) { 
      case R.id.aboutusButton: 
       fragment = new AboutFragment(); 
       replaceFragment(fragment); 
       break; 

      case R.id.phbookButton: 
       fragment = new PhoneBookFragment(); 
       replaceFragment(fragment); 
       break; 
     } 
    } 

    public void replaceFragment(Fragment someFragment) { 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     transaction.replace(R.id.frame_container, someFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 


} 
+0

Puoi darmi un esempio di implementazione nel mio codice con la tua risposta Per favore? – Firefog

+0

@Firefog aspetta. Sto modificando. –

+0

Grazie mille – Firefog

Problemi correlati