效果图
原理
- 底部导航栏使用BottomNavigationView + 自定义的ViewPager
- 底部导航栏使用BottomNavigationView + ViewPager
底部导航栏实现
简单的BottomNavigationView使用
布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<com.example.bibingwei.view.view.NoScrollViewPager
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>菜单文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_reading"
android:icon="@drawable/reading"
android:orderInCategory="0"
android:title="@string/title_reading"/>
<item
android:id="@+id/navigation_luck"
android:icon="@drawable/luck"
android:orderInCategory="1"
android:title="@string/title_luck"/>
<item
android:id="@+id/navigation_music"
android:icon="@drawable/music"
android:orderInCategory="2"
android:title="@string/title_music"/>
<item
android:id="@+id/navigation_video"
android:icon="@drawable/video"
android:orderInCategory="3"
android:title="@string/title_video"/>
</menu>
自定义ViewPager
注意拦截事件和事件处理事件返回false
,这样就避免冲突(事件分发机制)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class NoScrollViewPager extends ViewPager{
public NoScrollViewPager(@NonNull Context context) {
super(context);
}
public NoScrollViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
Activity实现
1 | public class MainActivity extends AppCompatActivity { |
顶部导航栏实现
布局文件
1 | "1.0" encoding="utf-8" xml version= |
菜单文件
1 | <?xml version="1.0" encoding="utf-8"?> |
Fragment实现
1 | public class reading_fragment extends Fragment { |
参考
https://tomoya92.github.io/2017/04/05/android-bottomnavigationview-viewpager-fragment/
https://www.jianshu.com/p/2c51ac813eee