簡體   English   中英

通過單擊一個片段中的按鈕並更改android studio中的底部導航圖標,從一個片段重定向到另一個片段

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

通過單擊一個片段中的按鈕從一個片段重定向到另一個片段,我也必須更改底部的導航圖標。我的第一個片段`

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;
}}

我的第二個片段

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;
}}

這是我的主要活動

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();
}}

我必須通過單擊願望清單片段中的btncart按鈕將其重定向到mycart片段,頁面已重定向,但底部導航圖標仍保留在“願望清單”圖標上,而未更改為Mycart圖標。 圖片是心願 單屏幕 截圖 MyCart屏幕截圖

我該如何解決這個問題?

嘗試使用BottomNavigationView.setSelectedItemId(itemId)

設置所選的菜單項ID。 這與點擊項目的行為相同。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM