我有一个带有3个片段的选项卡布局,也有一个BottomNavigation。那么我应该将BottomNavigation放在3个片段的Activity中还是在每个片段中放置底部导航?因为我在每个Fragment中都有一个ListView,所以我认为每个Fragment应该具有一个BottomNavigation用于click事件。
答案 0 :(得分:0)
所有片段只需一个BottomNavigation。 您可以轻松使用此library,然后在YOUR_ACTIVITY_LAYOUT中进行如下设置:
<RelativeLayout
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">
<FrameLayout
android:layout_above="@id/bottom_navigation"
android:name="com.ali.digikalaapp.Fragment_pager_one"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.ss.bottomnavigation.BottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary">
<com.ss.bottomnavigation.TabItem
android:id="@+id/tab_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:tab_text="Home"
app:tab_icon="@drawable/ic_home_white_24dp"
/>
<com.ss.bottomnavigation.TabItem
android:id="@+id/tab_images"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:tab_text="Images"
app:tab_icon="@drawable/ic_image_black_24dp"
/>
<com.ss.bottomnavigation.TabItem
android:id="@+id/tab_camera"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:tab_text="Camera"
app:tab_icon="@drawable/ic_camera_white_24dp"
/>
</com.ss.bottomnavigation.BottomNavigation>
</RelativeLayout>
如果您有3个Fragment,请在YOUR_ACTIVITY中替换这样的片段:
public class YOUR_ACTIVITY extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_ACTIVITY_LAYOUT);
BottomNavigation bottomNavigation = (BottomNavigation) findViewById(R.id.bottom_navigation);
bottomNavigation.setOnSelectedItemChangeListener(new OnSelectedItemChangeListener() {
@Override
public void onSelectedItemChanged(int itemId) {
switch (itemId) {
case R.id.tab_home:
Fragment_Home home = new Fragment_Home();
FragmentManager fragment = getSupportFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.replace(R.id.frameLayout, home);
break;
case R.id.tab_images:
Fragment_Image image = new Fragment_Image();
FragmentManager fragment = getSupportFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.replace(R.id.frameLayout, image);
break;
case R.id.tab_camera:
Fragment_Camera camera = new Fragment_Camera();
FragmentManager fragment = getSupportFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.replace(R.id.frameLayout, camera);
break;
}
transaction.commit();
}
}
}
这是一个片段示例:
public class Fragment_Home extends Fragment {
public Fragment_Home() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
这是fragment_home 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=".Fragment.Fragment_Home">
<TextView
android:text="Blank Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>