我是编程新手,正在使用bottomNavigationView创建一个应用程序。在“欢迎”活动中,我有OnItemSelectedListener。但是,当我在Android设备上运行该应用程序并选择一个选项时,它不会替换片段,而是一次又一次地启动“欢迎活动”。
这是一个欢迎活动,无论我从BottonNavigationView中选择什么项目,它都将继续启动。
public class Welcome extends AppCompatActivity {
private Context mContext = Welcome.this;
private static final String TAG = "Welcome";
BottomNavigationViewEx bottomNav;
private List<ListItems> listItems;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Log.d(TAG, "onCreate: starting");
setupBottomNavigationView();
Toast.makeText(this, "Starting", Toast.LENGTH_SHORT).show();
enableNavigation();
HomeFragment homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
}
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
bottomNav = findViewById(R.id.nav_bottom);
BottomNavigationViewHelper.setUpNavigationView(bottomNav);
}
private void enableNavigation(){
bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.btnHome:
bottomNav.setSelectedItemId(R.id.btnHome);
HomeFragment homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
break;
case R.id.btnChats:
bottomNav.setSelectedItemId(R.id.btnChats);
ChatsFragment chatsFragment = new ChatsFragment();
android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
manager1.beginTransaction().replace(R.id.container, chatsFragment).commit();
break;
case R.id.btnPost:
bottomNav.setSelectedItemId(R.id.btnPost);
PostsFragment postsFragment = new PostsFragment();
android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
manager2.beginTransaction().replace(R.id.container, postsFragment).commit();
break;
case R.id.btnFavourites:
bottomNav.setSelectedItemId(R.id.btnFavourites);
FavouritesFragment favouritesFragment = new FavouritesFragment();
android.support.v4.app.FragmentManager manager3 = getSupportFragmentManager();
manager3.beginTransaction().replace(R.id.container, favouritesFragment).commit();
break;
case R.id.btnProfile:
bottomNav.setSelectedItemId(R.id.btnProfile);
ProfileFragment profileFragment = new ProfileFragment();
android.support.v4.app.FragmentManager manager4 = getSupportFragmentManager();
manager4.beginTransaction().replace(R.id.container, profileFragment).commit();
break;
}
return false;
}
});
}
}
这是layout_welcome文件
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.matt.mustjob.Home.Welcome">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/nav_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#431C60"
app:menu="@menu/menu"
app:itemIconTint="#fff"
app:itemTextColor="#FFF"/>
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:3)
您需要更改的几件事:
1)不要将ViewPager
用作Fragment
容器,有一种方法可以将Fragments
与ViewPager
一起使用并在{{1}中进行管理}。在Google上搜索“ viewpager及其android中的片段示例”以了解如何使用它。如果不需要Adapter
功能,则可以使用ViewPager
作为容器。
2)您无需将FrameLayout
保存在Context
中,可以始终使用Activity
,getApplicationContext()
或仅使用getBaseContext()
(或this
(取决于范围)
3)无需每次都致电YourActivity.this
。您可以调用它一次,并将引用分配给getSupportFragmentManager();
中的成员:
Activity
4)处理private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
manager = getSupportFragmentManager()
}
// Somewhere else:
manager.beginTransaction()...
事件后,您需要返回true;
,所以不要像在每次选择后都调用onNavigationItemSelected
一样进行操作:
break;
答案 1 :(得分:0)
除了@Hed Shafran的答案之外,请尝试以下方法:
检查您的logcat。当应用崩溃时,某些设备可能不会显示ANR对话框。 一次又一次地启动“欢迎活动” 的情况可能是由于出现错误导致应用程序停止工作了。