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

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. 我必须通过单击愿望清单片段中的btncart按钮将其重定向到mycart片段,页面已重定向,但底部导航图标仍保留在“愿望清单”图标上,而未更改为Mycart图标。 Images are Wishlist screenshot MyCart screen shot 图片是心愿 单屏幕 截图 MyCart屏幕截图

How can i solve this issue? 我该如何解决这个问题?

Try to use BottomNavigationView.setSelectedItemId(itemId) 尝试使用BottomNavigationView.setSelectedItemId(itemId)

Set the selected menu item ID. 设置所选的菜单项ID。 This behaves the same as tapping on an item. 这与点击项目的行为相同。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用导航情节提要板在片段中添加按钮以从一个片段跳转到另一个片段 - Unable to add a button in fragment using Navigation storyboard to jump from one fragment to another fragment 底部导航栏在打开另一个片段时停止运行 - Bottom navigation bar stop a fragment running when another one is opened 使用底部导航栏从一个片段移动到另一个片段时重置数据 - Data reset when moving from one to another fragment using Bottom Navigation Bar 通过在另一个活动中单击按钮将项目添加到ListView中的一个片段中 - Add items to ListView in a fragment in one Activity by clicking a button in another activity 片段通过滑动以及单击按钮从一个片段移动到另一个片段 - Fragment move from one Fragment to another by sliding as well as button click 使用Android Studio将对象或数组列表从一个片段传递到另一个片段 - Passing object or Array List from one Fragment to another Fragment using Android Studio 如何在点击返回按钮时从一个片段中调用另一个片段? - How to call another fragment from one fragment on hitting back button? Android-将日期从一个片段添加到另一个正在运行的片段 - Android - add date from one fragment to another running fragment 如何从Android中的另一个片段调用一个片段方法 - How to call one Fragment Method from another Fragment in android 如何从Android中的另一个片段类调用一个片段的方法 - how to call method of one fragment from another fragment class in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM