【发布时间】:2017-12-28 20:56:11
【问题描述】:
我的主要活动中有一个底部导航栏。通过单击底部导航中的选项卡之一,我想更改视图中的片段。我有以下代码: 主要活动:
public class StartActivity extends AppCompatActivity {
SwiftFragment swiftFragment;
FrameworksFragment frameworksFragment;
FrameLayout content;
android.support.v4.app.FragmentManager fragmentManager;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_home:
Log.i("Nachricht", "HOME");
return true;
case R.id.navigation_swift:
Log.i("Nachricht", "SWIFT");
transaction.replace(android.R.id.content, swiftFragment);
transaction.commit();
return true;
case R.id.navigation_frameworks:
Log.i("Nachricht", "FRAMEWORKS");
transaction.replace(android.R.id.content, frameworksFragment);
transaction.commit();
return true;
case R.id.navigation_profile:
Log.i("Nachricht", "PROFILE");
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
swiftFragment = (SwiftFragment) android.support.v4.app.Fragment.instantiate(this, SwiftFragment.class.getName(), null);
frameworksFragment = (FrameworksFragment) android.support.v4.app.Fragment.instantiate(this, FrameworksFragment.class.getName(), null);
content = (FrameLayout) findViewById(android.R.id.content);
fragmentManager = getSupportFragmentManager();
}
}
我的一个片段:
public class SwiftFragment extends Fragment {
ArrayList<ModelTutorial> items;
ListView list;
ArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_swift, container, false);
//TODO: Get Firebase Data
items = new ArrayList<>();
items.add(new ModelTutorial("Swift Anlegen der Liste", "TableView", "Test Test Test", null, null));
items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null));
items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null));
list = (ListView) view.findViewById(R.id.swiftTutorialsList);
adapter = new ListAdapter(getActivity(), items);
list.setAdapter(adapter);
return view;
}
}
如果我单击其中一个选项卡,则会显示正确的片段,所以这是有效的。但是,当新片段出现并且我想单击另一个选项卡以显示另一个片段时,这不起作用。底部导航栏不会对点击做出反应。即使Log.i 语句也不起作用,所以似乎没有调用OnNavigationItemSelectedListener。
我对 android 开发完全陌生,我不明白为什么这不起作用。如果有人可以帮助我解决我的问题,我将不胜感激。
编辑:XML 文件启动活动:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.marcelspindler.codingtutorials.StartActivity">
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
XML 文件片段:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.marcelspindler.codingtutorials.SwiftFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/swiftTutorialsList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
【问题讨论】:
-
你也可以添加xml文件吗?检查你的监听器是否真的被调用但返回 false,在这种情况下它不会记录。
-
需要 xml 布局,我认为这将是 xml 中的错误,因为当您使用
android.R.id.content替换片段时,它将替换整个窗口,例如通过为片段背景添加颜色来检查 -
我编辑了我的问题并添加了 XML 文件。 @RickSanchez 我试图在它返回 false 时记录,但这从未被调用过。 @HermanSTobi 我改变了背景颜色,你是对的,它覆盖了整个窗口,也覆盖了底部导航栏。如果
android.R.id.content不起作用,您知道避免这种情况的方法吗?
标签: android android-fragments android-bottomnav