Android 之路49---ViewPager控件

本文详细介绍如何使用ViewPager结合Fragment实现常见的App引导页效果及底部导航栏功能,并提供了完整的代码实例。

导读

1.简介
2.最简单的ViewPager实现
3.App引导页的实现
4.Fragment配合ViewPager
5.实现底部导航

简介

应用场景

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

两种引用android.support Library的方法

这里写图片描述

guild文件中依赖设置

这里写图片描述

最简单的ViewPager实现

这里写图片描述

配置文件 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.air.viewpager">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ImageViewPagerAdapter">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主页面布局文件 activity_image_view_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>

第一页面布局文件 first.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/app_name"/>
</android.support.constraint.ConstraintLayout>

第二页面布局文件 second.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="mmp"/>

</android.support.constraint.ConstraintLayout>

第三页面布局文件 third.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fuck you"/>
</android.support.constraint.ConstraintLayout>

java文件 ImageViewPagerAdapter.java

package com.example.air.viewpager;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

//注意这里改掉mainActivity后要记得把文件名和配置文件里的注册也改掉
public class ImageViewPagerAdapter extends AppCompatActivity {

    private ViewPager viewPager;
    private int[] mLayoutIDs={
            R.layout.first,
            R.layout.second,
            R.layout.third,
    };
    private List<View> mViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view_adapter);

        viewPager = (ViewPager)findViewById(R.id.view_pager);
        //数据
        mViews=new ArrayList<>();
        for (int i = 0; i < mLayoutIDs.length; i++) {
            final View view = getLayoutInflater().inflate(mLayoutIDs[i], null);
            mViews.add(view);
        }
        viewPager.setAdapter(mpagerAdapter);
    }


    PagerAdapter mpagerAdapter=new PagerAdapter() {
        /**
         * 数据的数量
         * @return
         */
        @Override
        public int getCount() {
            return mLayoutIDs.length;
        }


        /**
         * 检查view与object是否匹配
         * @param view
         * @param object 来自instantiateItem返回值
         * @return 布尔值
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }


        /**
         * 将视图添加进去
         * @param container
         * @param position
         * @return
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View child=mViews.get(position);
            container.addView(child);
            return child;
        }

        /**
         * 将视图释放掉
         * @param container
         * @param position
         * @param object
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViews.get(position));
        }
    };
}

显示结果

这里写图片描述

这里写图片描述

App引导页的实现

这里写图片描述

配置文件同上

布局文件 activity_image_view_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <LinearLayout
        android:id="@+id/dot"
        android:layout_width="120dp"
        android:layout_height="30dp"
        android:layout_marginBottom="30dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"></LinearLayout>rL

</android.support.constraint.ConstraintLayout>

java文件 ImageViewPagerAdapter.java

package com.example.air.viewpager;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

//注意这里改掉mainActivity后要记得把文件名和配置文件里的注册也改掉
public class ImageViewPagerAdapter extends AppCompatActivity {

    private ViewPager viewPager;
    private List<View> mViews;
    private ViewGroup mDotViewGroup;
    private List<ImageView> mDotViews=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view_adapter);

        viewPager = (ViewPager)findViewById(R.id.view_pager);
        mDotViewGroup=(ViewGroup)findViewById(R.id.dot);
        //数据
        mViews=new ArrayList<>();
        for (int i = 0; i < 3; i++) {
           ImageView imageView =new ImageView(this);
           imageView.setImageResource(R.mipmap.ic_launcher_round);
           mViews.add(imageView);

           ImageView dot=new ImageView(this);
           dot.setImageResource(R.drawable.dot);
           dot.setMaxHeight(100);
           dot.setMaxWidth(100);

           //宽高
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(20,20);
            //每个点距左边20一直排下去
            layoutParams.leftMargin=20;
            dot.setLayoutParams(layoutParams);
            dot.setEnabled(false);
            mDotViewGroup.addView(dot);
            mDotViews.add(dot);
        }
        viewPager.setAdapter(mpagerAdapter);
        //在屏幕上至少有四个页面不会被释放掉,而是缓存起来
        viewPager.setOffscreenPageLimit(4);
        //设置首次进入时页面的位置(当前在第一个位置)
        viewPager.setCurrentItem(0);
        setDotViews(0);
        //view的监听事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //当页面滑动时的反应
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //当页面被选择时的反应
            @Override
            public void onPageSelected(int position) {
                setDotViews(position);
            }

            //当页面由静止变滑动或由滑动变静止时的反应
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setDotViews(int position) {
        for(int index=0;index<mDotViews.size();index++){
            mDotViews.get(index).setImageResource(position==index? R.drawable.dot:R.mipmap.ic_launcher);
        }
    }


    PagerAdapter mpagerAdapter=new PagerAdapter() {
        /**
         * 数据的数量
         * @return
         */
        @Override
        public int getCount() {
            return 3;
        }


        /**
         * 检查view与object是否匹配
         * @param view
         * @param object 来自instantiateItem返回值
         * @return 布尔值
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }


        /**
         * 将视图添加进去
         * @param container
         * @param position
         * @return
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View child=mViews.get(position);
            container.addView(child);
            return child;
        }

        /**
         * 将视图释放掉
         * @param container
         * @param position
         * @param object
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViews.get(position));
        }
    };
}

显示结果

这里写图片描述

Fragment配合ViewPager

这里写图片描述

配置文件 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.air.viewpager">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TabViewPagerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主页面布局文件 activity_tab_viewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>

Fragment布局文件 fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="testView"
        android:textSize="36sp" />
</android.support.constraint.ConstraintLayout>

Fragment java文件 TestFragment.java

package com.example.air.viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by air on 2018/4/17.
 */

//注意这里extends的Fragment是v4包里的
public class TestFragment  extends Fragment {

    public static final String POSITION = "position";
    private String position;

    public static TestFragment newInstance(int position){
        TestFragment fragment=new TestFragment();
        Bundle bundle=new Bundle();
        bundle.putInt(POSITION,position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null){
            position = String.valueOf(getArguments().getInt(POSITION));
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_test,null);
        //注意这里findViewById前边的view不能省
        TextView textView=(TextView)view.findViewById(R.id.text_view);
        textView.setText(position);
        return view;
    }
}

主页面java文件 TabViewPagerActivity.java

package com.example.air.viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

/**
 * 用Fragment的优点是比用Activity更简单
 * Created by air on 2018/4/17.
 */

public class TabViewPagerActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_viewpager);

        ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return TestFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return 4;
            }
        });
    }
}

显示结果

这里写图片描述

这里写图片描述

实现底部导航

这里写图片描述

配置文件 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.air.viewpager">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TabViewPagerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

color资源文件 color_main_tab_txt.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="#cccccc"/>
    <item android:state_selected="true" android:color="#4dd0c8"/>
    <item android:state_pressed="true" android:color="#4dd0c8"/>
</selector>

图片资源文件⚠️这里要有图片资源哦

main_tab_icon_home.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--这里三行代码的顺序很重要,它会自动从上往下依次检索
    如果当前状态更常用就直接应用,即如果把第三行放在第一行会出现
    点击tab不会变化的情况,就是因为它用了默认的第一行状态-->
    <item android:state_selected="true" android:drawable="@drawable/tabbar_home_pressed"/>
    <item android:state_pressed="true" android:drawable="@drawable/tabbar_home_pressed"/>
    <item android:drawable="@drawable/tabbar_home"  />

</selector>

main_tab_icon_message.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@drawable/tabbar_msg_pressed"/>
    <item android:state_pressed="true" android:drawable="@drawable/tabbar_msg_pressed"/>
    <item android:drawable="@drawable/tabbar_msg"/>

</selector>

main_tab_icon_me.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@drawable/tabbar_my_pressed"/>
    <item android:state_pressed="true" android:drawable="@drawable/tabbar_my_pressed"/>
    <item android:drawable="@drawable/tabbar_my"/>

</selector>

主页面布局文件 activity_tab_viewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<!--TabHost是专门用来处理导航页面的-->
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_host"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <!--Fragment是布局要求必须有的,但它放在viewPager下边不用显示-->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:layout_above="@+id/tab_divider"
            ></FrameLayout>

        <!--view的作用是做一条横线区分上下两部分-->
        <View
            android:id="@+id/tab_divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#dfdfdf"
            android:layout_above="@android:id/tabs"
            />

        <!--tabWidget是下方的导航-->
        <!--showDividers表示是否显示分割线-->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:showDividers="none"></TabWidget>


    </RelativeLayout>

</TabHost>

Fragment 布局文件 fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="testView"
        android:textSize="36sp" />
</android.support.constraint.ConstraintLayout>

Tab的布局文件 main_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="4dp"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textColor="@color/color_main_tab_txt"
            android:text="pager"/>

    </LinearLayout>



</RelativeLayout>

⚠️这里要alt+enter 建一个文件,如图选择第一项建一个单独文件,第二项建在values文件里
这里写图片描述

这里写图片描述
color是这个文件所在包

这里写图片描述
注意这里文件要与图片资源在一个包里

Fragment java文件 TestFragment.java

package com.example.air.viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by air on 2018/4/17.
 */

//注意这里extends的Fragment是v4包里的
public class TestFragment  extends Fragment {


    public static final String TITLE = "title";
    private String mTitle;

    public static TestFragment newInstance(String title){
        TestFragment fragment=new TestFragment();
        Bundle bundle=new Bundle();
        bundle.putString(TITLE,title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null){
            mTitle = getArguments().getString(TITLE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_test,null);
        //注意这里findViewById前边的view不能省
        TextView textView=(TextView)view.findViewById(R.id.text_view);
        textView.setText(mTitle);
        return view;
    }
}

主页面java文件 TabViewPagerActivity.java

package com.example.air.viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * 用Fragment的优点是比用Activity更简单
 * Created by air on 2018/4/17.
 */

public class TabViewPagerActivity extends AppCompatActivity implements TabHost.TabContentFactory{

    private TabHost tabHost;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_viewpager);


        //初始化总布局
        tabHost = (TabHost)findViewById(R.id.tab_host);
        tabHost.setup();

        //三个Tab处理
        //1.init data
        int[] titleIDs={
                R.string.home,
                R.string.message,
                R.string.me
        };

        int[] drawableIDs={
                R.drawable.main_tab_icon_home,
                R.drawable.main_tab_icon_message,
                R.drawable.main_tab_icon_me
        };

        //2. data <--> view
        for(int index=0;index<titleIDs.length;index++){
            View view=getLayoutInflater().inflate(R.layout.main_tab_layout,null,false);

            ImageView icon=(ImageView) view.findViewById(R.id.iv);
            TextView title=(TextView) view.findViewById(R.id.tv);
            View tab=(View)view.findViewById(R.id.tab_bg);

            icon.setImageResource(drawableIDs[index]);
            title.setText(titleIDs[index]);

            tab.setBackgroundColor(getResources().getColor(R.color.white));
            tabHost.addTab(
                    tabHost.newTabSpec(getString(titleIDs[index]))
                    .setIndicator(view)
                    .setContent(this)
            );
        }

        //三个Fragment组成的viewpager
        final Fragment[] fragments=new Fragment[]{
                TestFragment.newInstance("home"),
                TestFragment.newInstance("message"),
                TestFragment.newInstance("me")
        };


        final ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });

        //添加viewpager改变的互动事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //viewpager动时下边tab跟着动
            @Override
            public void onPageSelected(int position) {
                if(tabHost!=null){
                    tabHost.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //添加tab改变的互动事件
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if(tabHost!=null){
                    int position=tabHost.getCurrentTab();
                    viewPager.setCurrentItem(position);
                }
            }
        });

    }

    //这里是实现TabHost.TabContentFactory接口后需要的函数
    //用来放在tabHost.addTab(setContent)里边的
    //但setContent里边是已建好的viewpager,所以这里随便建一个就好
    @Override
    public View createTabContent(String tag) {
        View view=new View(this);
        view.setMinimumHeight(0);
        view.setMinimumWidth(0);
        return view;
    }
}

显示结果
这里写图片描述

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值