[英]Pass value from fragment to fragment through activity with bottom navigation view
[英]Fragment not loading with bottom navigation view
嗨,我正在制作應用程序,在其中我使用底部導航來瀏覽片段,但是我的問題是,當我單擊該應用程序時,加載的片段在框架布局中不可見,即使當我單擊底部導航時,它也只在下面顯示空白頁是我在Google上搜索此問題的主要活動代碼,但看起來我是唯一有此問題的人
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
FrameLayout frameLayout;
FragmentManager fragmentManager;
Fragment fragment;
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.frame_container)!=null){
if(savedInstanceState!=null){
return;
}
}
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
//bottomNavigationView.setOnNavigationItemSelectedListener(this);
//final SeekBar sb = (SeekBar) findViewById(R.id.sb);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
private void loadFragment(Fragment fragment) {
// load fragment
//HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment,null);
// transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
}
}
和我的xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
app:menu="@menu/navigation"
android:backgroundTint="@color/nav_color"
app:itemIconTint="@color/bottom_nav_fore_color"
android:background="?android:attr/windowBackground"
android:layout_weight="0.1">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
您的代碼似乎正確,請取消注釋此行,然后檢查:
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = ItemOneFragment.newInstance();
break;
case R.id.action_item2:
selectedFragment = ItemTwoFragment.newInstance();
break;
case R.id.action_item3:
selectedFragment = ItemThreeFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, selectedFragment);
transaction.commit();
return true;
}
});
希望這可以幫助 !
刪除此:
implements BottomNavigationView.OnNavigationItemSelectedListener
您沒有設置setOnNavigationItemSelectedListener
意味着您沒有將片段設置為底部導航抽屜,這就是為什么沒有片段在查看的原因。
首先將setOnNavigationItemSelectedListener
添加setOnNavigationItemSelectedListener
導航
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
然后添加以下代碼:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.