为了将之前做安卓项目的一些有用的东西记录下来,本人开通了博客,这是第一篇。大家都知道,微信的主界面是采用ViewPager结合Fragment实现的,这样实现的好处我就不多说了,而ViewPager和Fragment的有关知识在网上很多,大家可以自行去查找。本篇的主要内容就是用Android Studio实现这个功能。
首先,我们自定义一个CustomViewPager类,它继承于ViewPager,这是为了方便我们在代码中多次利用。我在这个类中限制了划屏切换页卡的功能,只是个人觉得划来划去比较眼花。下面是这个类的代码:
然后,为了实现四个tab,写一个TabBarCellView来装载一个子按钮的内容,具体的类和布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp" >
<ImageView
android:id="@+id/tab_bar_imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tab_bar_textView"
android:layout_marginBottom="0dp"
android:layout_marginTop="3dp"
android:contentDescription="@string/app_name"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tab_bar_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:gravity="center"
android:text="@string/test"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<Button
android:id="@+id/tab_bar_newsButton"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:visibility="gone" />
</RelativeLayout>
接着写一个TabBarView来装载这四个TabBarCellView,这个类以及它的布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:background="#fff"
>
<com.module.views.TabBarCellView
android:id="@+id/tabBarCellView0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.module.views.TabBarCellView
android:id="@+id/tabBarCellView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.module.views.TabBarCellView
android:id="@+id/tabBarCellView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.module.views.TabBarCellView
android:id="@+id/tabBarCellView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
做好前面的准备工作之后,我们开始写装载四个Fragment的MainActivity了,具体的代码以及它的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.module.views.CustomViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/main_line" >
</com.module.views.CustomViewPager>
<View
android:id="@+id/main_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@+id/home_tabBarView"
android:background="#333333" />
<com.module.views.TabBarView
android:id="@+id/home_tabBarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
在MainActivity中我们自定义了一个适配器,即FragmentAdapter,这样方面我们添加一些自己想要的功能。
完成上述步骤之后,就是写这四个Fragment的内容了,其实Fragment以及它的布局和Activity的大同小异,这里就不赘述,因此这四个页卡的内容就留给有需要的人自己去填充了。以上就是本篇博客的全部内容,希望能帮到大家。谢谢!