简体   繁体   中英

redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio

Redirection from one fragment to another by clicking a button in one fragment ,i have to change the bottom navigation icon also.My first fragment`

public class WishList extends Fragment {
public static WishList newInstance() {
    WishList fragment = new WishList();
    return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView= inflater.inflate(R.layout.fragment_wish_list, container, false);
    Button btncart=(Button) rootView.findViewById(R.id.btncart);
    btncart.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            FragmentTransaction fr=getFragmentManager().beginTransaction();
            fr.replace(R.id.frame_layout,new MyCart());
            fr.commit();
        }
    });
    return rootView;
}}

My second fragment

public class MyCart extends Fragment {
public static MyCart newInstance() {
    MyCart fragment = new MyCart();
    return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView= inflater.inflate(R.layout.fragment_my_cart, container, false);
    Button paynow = (Button) rootView.findViewById(R.id.btnpaynow);
    paynow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), PaymentMethod.class);
            startActivity(intent);
        }
    });
    Button upload = (Button) rootView.findViewById(R.id.btnUpload);
    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), YourPrescription.class);
            startActivity(intent);
        }
    });
    return rootView;
}}

This is my main activity

public class MyAccount extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

    navigation.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item)
                {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.navigation_home:
                            selectedFragment = Home.newInstance();
                            break;
                        case R.id.navigation_account:
                            selectedFragment = Account.newInstance();
                            break;
                        case R.id.navigation_dashboard:
                            selectedFragment = MyCart.newInstance();
                            break;
                        case R.id.navigation_notifications:
                            selectedFragment = WishList.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });
    String parameter = getIntent().getStringExtra("PARAMETER");
    String parameter1 = getIntent().getStringExtra("PARAMETER1");

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (parameter != null)
    {
        transaction.replace(R.id.frame_layout, WishList.newInstance());
    }
    else
        {
            if(parameter1 !=null ) { transaction.replace(R.id.frame_layout, MyCart.newInstance()); }
            else { transaction.replace(R.id.frame_layout, Home.newInstance()); }
        }
    transaction.commit();
}}

i have to redirect to mycart fragment by clicking button btncart in wishlist fragment ,the page is redirected but the bottom navigation icon remains on the Wishlist icon and not changes to Mycart icon. Images are Wishlist screenshot MyCart screen shot

How can i solve this issue?

Try to use BottomNavigationView.setSelectedItemId(itemId)

Set the selected menu item ID. This behaves the same as tapping on an item.

https://developer.android.com/reference/android/support/design/widget/BottomNavigationView#setselecteditemid

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