J'ai un bouton sur mon HomeFragment, je veux ouvrir un fragment sur un clic de bouton à partir d'un fragment. Quelque chose comme la navigation. J'essaie avec le code ci-dessous mais je n'ai pas travaillé. S'il vous plaît aider! Je suis très nouveau sur Android et j'essaie d'apprendre de nouvelles choses.
Voici mon code
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;
}
}
Je cherche quelque chose comme du code ci-dessous, mais je ne sais pas comment faire en sorte que le code fonctionne avec mon code.
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;
}
}
Vous pouvez remplacer le fragment en utilisant FragmentTransaction en cliquant sur un bouton. Quelque chose comme ça:
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();
En savoir plus sur effectuer des transactions de fragment ici.
code:
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();
}
}
J'ai un truc simple pour ça! C'est le code dans MainActivity:
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
intent.putExtra("pos",1);
startActivity(intent);
Intent intent1=new Intent(getApplicationContext(),Main2Activity.class);
intent1.putExtra("pos",2);
startActivity(intent1);
Dans Main2Activity, qui est un NavigationDrawer avec des fragments
Bundle extras;
extras=getIntent().getExtras();
if(extras!=null)
{
position=extras.getInt("pos");
if(position==1)
{
fragment=new FragmentOne();
if(fragment !=null)
{
Android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area,fragment);
fragmentTransaction.commit();
}
}
if(position==2)
{
fragment=new FragmentTwo();
if(fragment !=null)
{
Android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area,fragment);
fragmentTransaction.commit();
}
}
}