*************************************MainActivity*****************************************
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 MyViewPagerIndicator myViewPagerIndicator;
private String[] titles = { "哈哈", "呵呵", "吼吼", "啦啦", "呀呀", "嘎嘎", "飞飞", "吱吱",
"唧唧", "歪歪", "哒哒", "QQ", "滴滴", "嘻嘻" };
private int[] pics = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewPagerIndicator = (MyViewPagerIndicator) findViewById(R.id.myViewPagerIndicator);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
myViewPagerIndicator.setViewPager(viewPager);
myViewPagerIndicator.addTitles(titles);
viewPager.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getCount() {
return pics.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(pics[position]);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object);
}
});
}
}
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyViewPagerIndicator extends HorizontalScrollView implements OnClickListener{
private LinearLayout linearLayout;
private String[] titles;
private ViewPager viewPager;
ArrayList<View> viewList = new ArrayList<View>();
public MyViewPagerIndicator(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
// TODO Auto-generated constructor stub
}
public MyViewPagerIndicator(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView();
}
private void initView() {
// TODO Auto-generated method stub
// 创建线性布局
linearLayout = new LinearLayout(getContext());
this.addView(linearLayout);
}
public MyViewPagerIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initView();
}
public MyViewPagerIndicator(Context context) {
super(context);
// TODO Auto-generated constructor stub
initView();
}
public void setViewPager(ViewPager viewPager) {
this.viewPager = viewPager;
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
setCurrentItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public void addTitles(String[] titles) {
this.titles = titles;
viewList.clear();
for (int i = 0; i < titles.length; i++) {
View view = View.inflate(getContext(), R.layout.titles_item, null);
TextView tv_titles = (TextView) view.findViewById(R.id.tv_tab);
TextView tv_line = (TextView) view.findViewById(R.id.tv_line);
viewList.add(view);
tv_titles.setText(titles[i]);
if (i == 0) {
tv_titles.setTextColor(Color.RED);
tv_line.setVisibility(View.VISIBLE);
} else {
tv_titles.setTextColor(Color.BLACK);
tv_line.setVisibility(View.GONE);
}
view.setId(i);
view.setOnClickListener(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(view, params);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setCurrentItem(v.getId());
}
private void setCurrentItem(int position) {
// TODO Auto-generated method stub
this.setScrollBarStyle(HorizontalScrollView.GONE);
int width = linearLayout.getChildAt(position).getWidth();
this.scrollTo(width * position, 0);
for (int i = 0; i < titles.length; i++) {
View view = viewList.get(i);
TextView tv_titles = (TextView) view.findViewById(R.id.tv_tab);
TextView tv_line = (TextView) view.findViewById(R.id.tv_line);
if (position == i) {
// 设置传递过来的viewpager的当前页
viewPager.setCurrentItem(i);
tv_titles.setTextColor(Color.RED);
tv_line.setVisibility(View.VISIBLE);
} else {
tv_titles.setTextColor(Color.BLACK);
tv_line.setVisibility(View.GONE);
}
}
}
}
<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" >
<com.example.day11zdyviewpager.MyViewPagerIndicator
android:id="@+id/myViewPagerIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
*******************************************titles_item布局***********************************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_tab"
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="哈哈哈哈" />
<TextView
android:id="@+id/tv_line"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#ff0000" />
</LinearLayout>
**************************************************OK*******************************************************