Есть базовое Acrivity, в которое есть Fragment_Go и Button go_button как по нажатию на эту кнопку перейти к Fragment_Train? В коде прописал в OnCreate и OnClick следующие строчки, приложение запускается без ошибок, но при нажатии кнопки ничего не происходит. Код Activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Fragment_Go fgo;
Fragment_Programs fprograms;
Fragment_Settings fsettings;
Fragment_Statistic fstatistic;
Fragment_Uprajnenia fuprajnenia;
Fragment_zameri fzameri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
fgo = new Fragment_Go();
if (fgo != null) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, fgo);
fragmentTransaction.commit();
}
fprograms = new Fragment_Programs();
fsettings = new Fragment_Settings();
fstatistic = new Fragment_Statistic();
fuprajnenia = new Fragment_Uprajnenia();
fzameri = new Fragment_zameri();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
FragmentTransaction ftrans = getFragmentManager().beginTransaction();
if (id == R.id.nav_go) {
ftrans.replace(R.id.container, fgo);
} else if (id == R.id.nav_programs) {
ftrans.replace(R.id.container, fprograms);
}else if (id == R.id.nav_uprajnenia) {
ftrans.replace(R.id.container, fuprajnenia);
}else if (id == R.id.nav_statistic) {
ftrans.replace(R.id.container, fstatistic);
} else if (id == R.id.nav_zameri) {
ftrans.replace(R.id.container, fzameri);
} else if (id == R.id.nav_settings) {
ftrans.replace(R.id.container, fsettings);
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
ftrans.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Код первого фрагмента:
public class Fragment_Go extends Fragment implements View.OnClickListener {
Fragment_Train f_train;
@Override
public void onClick(View view) {
FragmentTransaction ftrans = getFragmentManager().beginTransaction();
if (getId() == R.id.go_button) {
ftrans.replace(R.id.container, f_train);
}
ftrans.commit();
}
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public Fragment_Go() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment_Go.
*/
// TODO: Rename and change types and number of parameters
public static Fragment_Go newInstance(String param1, String param2) {
Fragment_Go fragment = new Fragment_Go();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment__go, container, false);
Button go_button = (Button) rootView.findViewById(R.id.go_button);
go_button.setOnClickListener(this);
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}