安卓第五天---将Fragment添加到ViewPager中并实现与底部导航栏的交互(简单的例子)

本文介绍了如何在Android应用中结合ViewPager和Fragment实现底部导航栏的交互。首先,在主页面设置ViewPager和底部导航栏,接着创建多个Fragment并将它们添加到List中供ViewPager使用。当点击底部导航栏的不同位置时,ViewPager会切换到相应的Fragment。同时,详细讲述了Fragment的布局设置、适配器FragmentViewPagerAdapter的创建以及底部栏的交互逻辑实现。
摘要由CSDN通过智能技术生成

首先,思路是在一个页面设置一个ViewPager,并且在底部添加一个导航栏!
然后创建几个Fragment,将Fragment添加到一个List列表中以便加入ViewPager的适配器中.
其次将底部的控件设置点击事件,点击底部导航栏的位置,可以是ViewPager切换到对应的位置上,以便于显示对应的Fragment.
最后设置ViewPager滑动于底部导航栏的交互.
在这里插入图片描述

首先,设置布局main_activity.xml代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_bar"></android.support.v4.view.ViewPager>

<TextView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_above="@id/bottom_bar"
    android:background="#000000" />

<LinearLayout
    android:id="@+id/bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:weightSum="3">


    <ImageView
        android:id="@+id/home_img"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFF5EE"
        android:padding="5dp"
        android:src="@drawable/home_unselected" />

    <ImageView
        android:id="@+id/star_img"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFF5EE"
        android:padding="5dp"
        android:src="@drawable/star_unselected" />

    <ImageView
        android:id="@+id/mine_img"
        android:layout_width="0dp"
        android:layout_height="matc
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值