ViewPagerIndicator(demo)
下载地址:
demo源码
效果图:
ViewPagerIndicator在项目中很常用。我直接在项目中把需要关联的包拷贝过来了,也可以直接关联Indicator的库。
布局:
<span style="font-size:18px;"><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=".MainActivity" ><com.example.xiangmuer.view.pagerindicator.TabPageIndicatorandroid:id="@+id/indicator"style="@style/Theme.PageIndicatorDefaults"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/><android.support.v4.view.ViewPagerandroid:id="@+id/viewpager"android:layout_width="fill_parent"android:background="#ffffff"android:layout_height="0dp"android:layout_weight="5" /></LinearLayout></span>
MainActivity:
<span style="font-size:18px;">package com.example.viewpagerincidator;import java.util.ArrayList;
import java.util.List;import com.example.xiangmuer.view.pagerindicator.TabPageIndicator;import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;public class MainActivity extends Activity {private MyPagerAdapter mypageradapter;private TabPageIndicator indicator;private ViewPager viewpager;private String[] text={"新闻","体育","娱乐"};private int[] arr={R.drawable.a,R.drawable.b,R.drawable.d};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initview();initdata();}private void initview() {viewpager=(ViewPager) findViewById(R.id.viewpager);indicator=(TabPageIndicator) findViewById(R.id.indicator);}private void initdata() {// TODO Auto-generated method stubmypageradapter=new MyPagerAdapter();viewpager.setAdapter(mypageradapter);//把indicator和viewpager進行綁定indicator.setViewPager(viewpager);}class MyPagerAdapter extends PagerAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn arr.length;}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0==arg1;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View)object);}@Overridepublic Object instantiateItem(ViewGroup container, int position) {View view=View.inflate(getApplicationContext(), R.layout.item, null);ImageView iv= (ImageView) view.findViewById(R.id.iv);iv.setImageResource(arr[position]);container.addView(view);// 一定不能少,将view加入到viewPager中return view;}//给指针去设置标题文字public CharSequence getPageTitle(int position) {//这个方法是怎么自动生成的return text[position];}}}
</span>