我的主要活动中有一个底部导航栏。通过单击我底部导航中的一个选项卡,我想更改视图中的片段。我有以下代码: 主要活动:
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文件StartActivity:
<?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>
答案 0 :(得分:4)
您需要将Fragment添加到MainActivity中的容器中。
尝试这样的事情:
<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">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<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>
然后将替换行替换为:
transaction.replace(R.id.fragment_container, fragment);
BONUS :你可以像这样发起你的片段:
swiftFragment = new SwiftFragment();
看完之后: Best practice for instantiating a new Android Fragment