我的主要活动中有一个底部导航栏.通过单击底部导航中的一个选项卡,我想更改视图中的片段.我有以下代码:
主要活动:
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; }};@OverrIDeprotected 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;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);}@OverrIDepublic 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>
解决方法:
您需要将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,片段);
奖励:您可以像这样启动片段:
swiftFragment =新的SwiftFragment();
之后看一下:
Best practice for instantiating a new Android Fragment
以上是内存溢出为你收集整理的底部的导航栏与片段全部内容,希望文章能够帮你解决底部的导航栏与片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)