黑马北京新闻项目连载(4)--->ViewPagerIndicator结合FragmentPagerAdapter(开源库的Git上的demo)

开源库和代码下载地址 点击打开链接

上篇是ViewPagerIndicator结合PagerAdapter 使用,这篇是ViewPagerIndicator结合FragmentPagerAdapter使用

另一篇 ViewPagerIndicator结合PagerAdapter使用

由于项目中用到这个开源库,所以将git上的demo,摘出来,先学习下,后续在写一篇添加pager的,这篇是fragment

黑马北京新闻项目连载(4)--->ViewPagerIndicator结合FragmentPagerAdapter(开源库的Git上的demo)_第1张图片


布局文件activity_main.xml    //布局线性布局 上面TabPageIndicator 下面ViewPager

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.viewpagerindicatordeafult.MainActivity" >

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

MainActivity

package com.example.viewpagerindicatordeafult;
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;

import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends FragmentActivity {
	//Tab栏的菜单数量--创建数据源
	  private static final String[] CONTENT = new String[] { "Recent", "Artists", "Albumssdfdsf", "Songs", "Playlsfsdfists", "Genres" };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//布局线性布局 上面TabPageIndicator 下面ViewPager
		setContentView(R.layout.activity_main);
		

		//获取ViewPager控件
		ViewPager pager = (ViewPager)findViewById(R.id.pager);
		//获取适配器
        FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());
        //设置适配器
        pager.setAdapter(adapter);

        //获取indicator控件
        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        //固定用法
        indicator.setViewPager(pager);
	}
	
	 class GoogleMusicAdapter extends FragmentPagerAdapter {
	        public GoogleMusicAdapter(FragmentManager fm) {
	            super(fm);
	        }

	        /**
	         * 获取每一个对应的,对应的,fragment
	         */
	        @Override
	        public Fragment getItem(int position) {
	            return TestFragment.newInstance(CONTENT[position]);
	        }
//获取Tab栏目标题的文字填充
	        @Override
	        public CharSequence getPageTitle(int position) {
	            return CONTENT[position];
	        }

	        //获取fragment数量
	        @Override
	        public int getCount() {
	          return CONTENT.length;
	        }
	    }
}

TestFragment

package com.example.viewpagerindicatordeafult;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

/**
 * fragment对象
 * 
 */
public final class TestFragment extends Fragment {
	private String mContent = "???";

	public static TestFragment newInstance(String content) {
		//构建需要创建的fragment的实例对象
		TestFragment fragment = new TestFragment();
		/**
		 * 设置fragment页面需要展现的内容
		 * 此时fragment内容为空的,还没有添加!!!!!!!!!!!!!
		 */
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < 25; i++) {
			builder.append(content).append(" ");
		}
		//赋值内容
		fragment.mContent = builder.toString();

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	/**
	 * 返回fragment的视图view
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//为了测试方便,这里直接展现一个TextView即可
		TextView text = new TextView(getActivity());
		//设置内容居中
		text.setGravity(Gravity.CENTER);
		//设置内容
		text.setText(mContent);
		//设置字体大小
		text.setTextSize(20 * getResources().getDisplayMetrics().density);
		//设置paddding值
		text.setPadding(20, 20, 20, 20);

		return text;
	}

}

activity的配置文件如下

 <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.PageIndicatorDefaults" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

这里主题是默认的,当然也可以自己定义

<resources>
    <style name="Theme.PageIndicatorDefaults" parent="android:Theme">
        <item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item>
    </style>

    <style name="Widget">
    </style>

    <style name="Widget.TabPageIndicator" parent="Widget">
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/vpi__tab_indicator</item>
        <item name="android:paddingLeft">22dip</item>
        <item name="android:paddingRight">22dip</item>
        <item name="android:paddingTop">12dp</item>
        <item name="android:paddingBottom">12dp</item>
        <item name="android:textAppearance">@style/TextAppearance.TabPageIndicator</item>
        <item name="android:textSize">12sp</item>
        <item name="android:maxLines">1</item>
    </style>

    <style name="TextAppearance.TabPageIndicator" parent="Widget">
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/vpi__dark_theme</item>
    </style>

</resources>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 分为4种图片 -->
    <item  android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/vpi__tab_unselected_holo" />
    
    <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/vpi__tab_unselected_pressed_holo" />
    
    <item  android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/vpi__tab_selected_holo" />
    <item  android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/vpi__tab_selected_pressed_holo" />
</selector>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#f00"/>
    <item android:state_selected="true" android:color="#f00"/>
    <item android:color="#fff"/>
</selector>



你可能感兴趣的:(黑马北京新闻项目连载(4)--->ViewPagerIndicator结合FragmentPagerAdapter(开源库的Git上的demo))