繁体   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