[英]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.