简体   繁体   中英

Button click from one fragment to another?

I am creating an app that will display cars. I am using the navigation drawer template and RelativeLayout.

I have one activity and a number of fragments.

Within my activity_main page (the page the user sees when the app first loads) I have a fragment (HomeFragment) with a picture of a car. I want to be able to click on the car and it takes me to another fragment where I can put more images of the same car on.

Does anyone know the code required to do this? I have heard I need to use FragmentManager but I'm not sure.

This is my solution:

  1. Create interface OnPictureOfCarClickListener :

     public interface OnPictureOfCarClickListener { void onPictureOfCarClicked(); } 
  2. Make your MainActivity implement OnPictureOfCarClickListener . It will require you @Override onPictureOfCarClicked() method:

     @Override public void onPictureOfCarClicked() { // this method will call when you click to the picture of car in your HomeFragment. // I will named fragment show more images is MoreCarFragment MoreCarFragment fragment = new MoreCarFragment(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragmentContainer, fragment); transaction.commit(); } 
  3. In your HomeFragment:

     public class HomeFragment extends Fragment { OnPictureOfCarClickListener mCallback; @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (OnPictureOfCarClickListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnPictureOfCarClickListener"); } // when you click to the picture of car public void onPictureOfCarClick(View view) { mCallback.onPictureOfCarClicked(); } } 

Hope it help!

follow an example of what I use in my project, each item in the case is a image in my layout.

 switch (v.getId()) {

        case R.id.bottom_navigation_help:
           selectedFragment = HelpFragment.newInstance();
            break;

        case R.id.bottom_navigation_start:
           selectedFragment = WalletFragment.newInstance();
           break;
        case R.id.bottom_navigation_client:
           selectedFragment = CustomerListFragment.newInstance();
            break;

        case R.id.bottom_navigation_statistics:
           selectedFragment = StatisticsFragment.newInstance();
            break;

        case R.id.bottom_navigation_comunication:
            walletPresenter.checkCommunicationStep();
            break;
    }

    if (selectedFragment != null) {
        hideProgressDialog();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragmentContent, selectedFragment);
        transaction.commit();
    }

You also need to add this to each 1 of your fragments

 @NonNull
public static Fragment newInstance() {
    return new ClientFragment();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM