我在网上跟随了一个教程来创建底部栏Navigation
,但是这个家伙并没有清楚地解释如何向每个片段中添加内容以及如何在片段之间进行交换。我想在首页上添加一个EditText
,作为有关如何向片段添加项目的示例。这是我的代码:
主要活动-
public class MainActivity extends AppCompatActivity {
private static final String TAG_FRAGMENT_CATA = "tag_frag_cata";
private static final String TAG_FRAGMENT_HOME = "tag_frag_home";
private static final String TAG_FRAGMENT_SETTINGS = "tag_frag_settings";
private BottomNavigationView bottomNavigationView;
private List<BottomBarFragment> fragments = new ArrayList<>(3);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bottombaritem_cata:
switchFragment(0, TAG_FRAGMENT_CATA);
return true;
case R.id.bottombaritem_home:
switchFragment(1, TAG_FRAGMENT_HOME);
return true;
case R.id.bottombaritem_settings:
switchFragment(2, TAG_FRAGMENT_SETTINGS);
return true;
}
return false;
}
});
buildFragmentsList();
switchFragment(1, TAG_FRAGMENT_HOME);
}
private void buildFragmentsList() {
BottomBarFragment cataFragment = buildFragment("Categories");
BottomBarFragment homeFragment = buildFragment("Home");
BottomBarFragment settingsFragment = buildFragment("Settings");
fragments.add(cataFragment);
fragments.add(homeFragment);
fragments.add(settingsFragment);
}
private BottomBarFragment buildFragment(String title) {
BottomBarFragment fragment = new BottomBarFragment();
Bundle bundle = new Bundle();
bundle.putString(BottomBarFragment.ARG_TITLE, title);
fragment.setArguments(bundle);
return fragment;
}
private void switchFragment(int pos, String tag) {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_fragmentholder, fragments.get(pos), tag).commit();
}
}
底部栏活动-
public class BottomBarFragment extends Fragment {
public static final String ARG_TITLE = "arg_title";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bottom_bar_fragment, container, false);
return rootView;
}
}
主要活动的XML
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:itemBackground="@android:color/white"
app:itemIconTint="@android:color/black"
app:itemTextColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottombar_menu" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/bottom_nav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/frame_fragmentholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
XML布局文件-
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BottomBarFragment">
</android.support.constraint.ConstraintLayout>
如果有人可以解释如何使用此代码,我将不胜感激!
答案 0 :(得分:1)
bottomNavigationBar中有三个片段。我猜这些将是目录,家庭和设置。对于每一个,您都需要创建一个新类。
例如对于cata:
public class CataFragment extends Fragment {
public CataFragment() {
// Required constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//here you would do the fragment specific coding
return inflater.inflate(R.layout.fragment_cata, container, false);
}
}
如您所见,它们与布局文件保持联系,这里是fragment_cata
文件,用于创建名称为xml的布局文件。布局文件可以是自动创建的空白片段布局,您可以在创建新文件时在列表中找到它。看起来像这样:
<RelativeLayout 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=".ChatFragment">
<!-- TODO: Update blank fragment layout -->
</RelativeLayout>
您可以像处理任何其他布局文件一样处理此布局文件