前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[android] 练习viewpagerindicator的使用(一)

[android] 练习viewpagerindicator的使用(一)

作者头像
唯一Chat
发布2019-09-10 16:35:41
5550
发布2019-09-10 16:35:41
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地

主要是学习一下使用这个库

activity_main.xml

代码语言:javascript
复制
<?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="match_parent"
    android:background="#eee"
    android:orientation="vertical" >
    <include layout="@layout/main_head"/>
    <com.viewpagerindicator.TabPageIndicator
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/vpi_tab"
        android:background="#C0D0E0">
    </com.viewpagerindicator.TabPageIndicator>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/vp_content"/>
</LinearLayout>

MainActivity.java

代码语言:javascript
复制
package com.example.csdn;



import com.viewpagerindicator.TabPageIndicator;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity {
    private TabPageIndicator tpi_tab;
    private ViewPager vp_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tpi_tab = (TabPageIndicator) findViewById(R.id.vpi_tab);
        vp_content = (ViewPager) findViewById(R.id.vp_content);
        FragmentManager fm = getSupportFragmentManager();
        TabAdapter adapter = new TabAdapter(fm);
        // ViewPager设置适配器
        vp_content.setAdapter(adapter);
        // 指示器绑定ViewPager
        tpi_tab.setViewPager(vp_content, 0);
    }

}

/**
 * 适配器
 * 
 * @author taoshihan
 * 
 */
class TabAdapter extends FragmentPagerAdapter {

    public static final String[] TITLES = new String[] { "业界", "移动", "研发" };

    public TabAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        return new Fragment();
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return TITLES[position % TITLES.length];
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-07-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档