Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> [android] 練習viewpagerindicator的使用(一),viewpagerindicator

[android] 練習viewpagerindicator的使用(一),viewpagerindicator

編輯:關於android開發

[android] 練習viewpagerindicator的使用(一),viewpagerindicator


主要是學習一下使用這個庫

activity_main.xml

<?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

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];
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved