ViewPagerIndicator
- 首先建一个类继承HorizontalScrollView重写三个构造
- 构造中一调二,二调三
布局
<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="1dp"
android:background="#ff0000" />
- 代码
package com.example.viewpagerindicator.view;
import java.util.ArrayList;
import java.util.List;
import com.example.viewpagerindicator.R;
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;
List<View> viewlist=new ArrayList<View>();
private ViewPager viewpager;
public MyViewPagerIndicator(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
//初始化
initView();
}
public MyViewPagerIndicator(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public MyViewPagerIndicator(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
//初始化
private void initView() {
// 创建线性布局
linearLayout = new LinearLayout(getContext());
this.addView(linearLayout);
}
//添加信息
public void addTitle(String[] titles ){
//全局传过来的值
this.titles=titles;
//清理集合
viewlist.clear();
//遍历
for (int i = 0; i < titles.length; i++) {
//填充布局
View view=View.inflate(getContext(), R.layout.item_xml, null);
//获取控件
TextView tv_tab=(TextView) view.findViewById(R.id.tv_tab);
TextView tv_line=(TextView) view.findViewById(R.id.tv_line);
//添加到集合
viewlist.add(view);
//赋值
tv_tab.setText(titles[i]);
//设置初始的下边线色
if (i==0) {
tv_tab.setTextColor(Color.RED);
tv_line.setVisibility(View.VISIBLE);
} else {
tv_tab.setTextColor(Color.BLACK);
tv_line.setVisibility(View.GONE);
}
//设置id
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 id) {
// TODO Auto-generated method stub
//获取到宽
int width = linearLayout.getChildAt(id).getWidth();
//设置HorizontalScrollView随着滑动变化
this.scrollTo(width*id, 0);
//遍历
for (int i = 0; i < titles.length; i++) {
View view = viewlist.get(i);
//找到控件
TextView tv_tab=(TextView) view.findViewById(R.id.tv_tab);
TextView tv_line=(TextView) view.findViewById(R.id.tv_line);
if (id==i) {
//设置当前的值
viewpager.setCurrentItem(i);
tv_tab.setTextColor(Color.RED);
tv_line.setVisibility(View.VISIBLE);
} else {
tv_tab.setTextColor(Color.BLACK);
tv_line.setVisibility(View.GONE);
}
}
}
@SuppressWarnings("deprecation")
public void setViewPager(ViewPager viewPager) {
// TODO Auto-generated method stub
this.viewpager=viewPager;
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
//设置当前的
setCurrentItem(position);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
});
}
}
布局
<com.example.viewpagerindicator.view.MyViewPagerIndicator
android:id="@+id/viewPagerIndicator"
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="wrap_content"
android:layout_weight="1"
>
</android.support.v4.view.ViewPager>
代码
package com.example.viewpagerindicator;
import com.example.viewpagerindicator.view.MyViewPagerIndicator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private MyViewPagerIndicator viewPagerIndicator;
private ViewPager viewPager;
private String[] titles = { "哈哈", "呵呵", "吼吼", "啦啦", "呀呀", "嘎嘎", "飞飞", "吱吱",
"唧唧", "歪歪", "哒哒", "QQ", "滴滴", "嘻嘻" };
private int[] pics={R.drawable.as3,R.drawable.as2,R.drawable.as1,R.drawable.as4,
R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.aa8,R.drawable.a9,
R.drawable.a10,R.drawable.as3,R.drawable.as2,R.drawable.as1,R.drawable.as4
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPagerIndicator = (MyViewPagerIndicator) findViewById(R.id.viewPagerIndicator);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerIndicator.addTitle(titles);
viewPagerIndicator.setViewPager(viewPager);
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(pics[position]);
imageView.setScaleType(ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
});
}
}