//继承LinearLayout
public class HorScollTablhost extends LinearLayout implements ViewPager.OnPageChangeListener {
private Context context;
private LinearLayout layout_th;
private ViewPager vp;
private List<String> stringList;
private List<Fragment> fragmentList;
private int color;
public HorScollTablhost(Context context) {
this(context,null);
}
public HorScollTablhost(Context context, @Nullable AttributeSet attrs) {
this(context,attrs,0);
}
public HorScollTablhost(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
/**
* 初始化控件
*/
private void init(Context context, AttributeSet attrs)
{
this.context=context;
//找定义属性(attrs,自定义属性的)
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HorScollTablhost);
color = typedArray.getColor(R.styleable.HorScollTablhost_zcolor, 0xff0099);
typedArray.recycle();
//找控件(找布局,自定义的布局赋给自定义的控件,确定)
View view = LayoutInflater.from(context).inflate(R.layout.hortablhost,this,true);//
layout_th = view.findViewById(R.id.layout_th);
vp = view.findViewById(R.id.vp);
//设置监听
vp.addOnPageChangeListener(this);
}
/**
* 设置参数(主方法传的)
*/
public void setData(List<String> stringList, List<Fragment> fragmentList)
{
this.stringList=stringList;
this.fragmentList=fragmentList;
// 绘制界面
draw();
}
/**
* 绘制界面
*/
private void draw() {
for (int i = 0; i < stringList.size(); i++) {
TextView tv=new TextView(context);
final int finalI = i;
//设置文字点击事件
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
vp.setCurrentItem(finalI);
}
});
//设置字间距(宽高)
LinearLayout.LayoutParams params = new LayoutParams(63, ViewGroup.LayoutParams.MATCH_PARENT);
//设置字体颜色
tv.setTextColor(getResources().getColorStateList(R.drawable.font_color));
//设置文字
tv.setText(stringList.get(i));
//居中
tv.setGravity(Gravity.CENTER);
//添加到LinearLayout
layout_th.addView(tv,params);
}
//第一个默认选中
TextView w = (TextView) layout_th.getChildAt(0);
w.setSelected(true);
//设置背景颜色(接收的颜色属性)
layout_th.setBackgroundColor(color);
//添加适配器frgment
FragmentManager fm=((FragmentActivity)context).getSupportFragmentManager();
Adapter adapter=new Adapter(fm,fragmentList);
vp.setAdapter(adapter);
}
/**
* 设置vp监听
* @param position
* @param positionOffset
* @param positionOffsetPixels
*/
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < stringList.size(); i++)
{
TextView childAt = (TextView) layout_th.getChildAt(i);
if (i==position) {
//设置选中状态
childAt.setSelected(true);
}
else
{
//设置选中状态
childAt.setSelected(false);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
定义适配器继承FragmentPagerAdapter
public class Adapter extends FragmentPagerAdapter{
private List<Fragment> fragmentList;
/*public void setdata(List<Fragment> fragmentList) {
this.fragmentList=fragmentList;
}*/
public Adapter(FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList=fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
主mainActivity函数中
HorScollTablhost h= (HorScollTablhost) findViewById(R.id.h);
//横划
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="50dip">
<LinearLayout
android:id="@+id/layout_th"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>