嗨我正在制作应用程序,我使用底部导航来浏览片段,但我的问题是当我点击应用程序加载片段在框架布局中不可见,甚至当我点击底部导航它只是显示空白页面 下面是我的主要活动代码 我在谷歌搜索这个问题,但看起来我是唯一一个有这个问题的人
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>
答案 0 :(得分:0)
您的代码似乎正确,取消注释此行,然后检查:
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
答案 1 :(得分:0)
您没有设置您的setOnNavigationItemSelectedListener
表示您没有将片段设置到底部导航抽屉,说明没有片段正在查看的原因。
首先将setOnNavigationItemSelectedListener
添加到bottomnavigation
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;
}
};