布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<com.example.a18_horizaontal_scroll.MyIndicator
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@android:color/holo_blue_bright"
android:id="@+id/indicador">
</com.example.a18_horizaontal_scroll.MyIndicator>
<android.support.v4.view.ViewPager
android:layout_below="@+id/indicador"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"/>
</RelativeLayout>
代码:MainActivity:
package com.example.a18_horizaontal_scroll;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private MyIndicator indicator;
private ViewPager viewPager;
private String[] titles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
indicator = (MyIndicator) findViewById(R.id.indicador);
viewPager = (ViewPager) findViewById(R.id.pager);
//1.设置标题
titles = new String[]{"推荐","北京","头条","军事","娱乐","八卦","图片","视频","小说","体育"};
indicator.setTitles(titles);
//2.viewpager设置适配器
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
NewsFragment newsFragment = new NewsFragment();
Bundle bundle = new Bundle();
bundle.putString("name",titles[position]);
newsFragment.setArguments(bundle);
return newsFragment;
}
@Override
public int getCount() {
return titles.length;
}
});
//3.指示器与viewpager关联
indicator.setViewPager(viewPager);
}
}
NewsFragment:
package com.example.a18_horizaontal_scroll;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* @author Dash
* @date 2017/9/19
* @description:
*/
public class NewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle arguments = getArguments();
TextView textView = new TextView(getActivity());
textView.setText(arguments.getString("name","推荐"));
return textView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
MyIndicator
package com.example.a18_horizaontal_scroll;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author Dash
* @date 2017/9/19
* @description:
*/
public class MyIndicator extends HorizontalScrollView implements ViewPager.OnPageChangeListener{
private static final int COLOR_TEXT_NORMAL = 0xFF000000;
private static final int COLOR_INDICATOR_COLOR = Color.BLACK;
private Context context;
private int tabWidth;
private String[] titles;
private int count;
private Paint mPaint;
private float mTranslationX;
private ViewPager viewPager;
private int SCREEN_WIDTH;
private float lineheight = 2.0f;
public MyIndicator(Context context) {
this(context, null);
}
public MyIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
this.context = context;
mPaint = new Paint();
mPaint.setColor(COLOR_INDICATOR_COLOR);
mPaint.setStrokeWidth(lineheight);//底部指示线的宽度
setHorizontalScrollBarEnabled(false);
SCREEN_WIDTH = context.getResources().getDisplayMetrics().widthPixels;
}
public void setLineheight(float height){
this.lineheight = height;
mPaint.setStrokeWidth(lineheight);//底部指示线的宽度
}
public void setViewPager(ViewPager viewPager){
this.viewPager = viewPager;
viewPager.addOnPageChangeListener(this);
}
public void setTitles(String[] titles){
this.titles = titles;
count = titles.length;
tabWidth = SCREEN_WIDTH/4;
generateTitleView();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
tabWidth = SCREEN_WIDTH/4;
}
@Override
protected void dispatchDraw(Canvas canvas)
{
super.dispatchDraw(canvas);
canvas.save();
canvas.translate(mTranslationX, getHeight() - lineheight);
canvas.drawLine(0, 0, tabWidth, 0, mPaint);//(startX, startY, stopX, stopY, paint)
canvas.restore();
}
public void scroll(int position, float offset)
{
mTranslationX = tabWidth * (position + offset);
scrollTo((int)mTranslationX-(SCREEN_WIDTH-tabWidth)/2, 0);
invalidate();
}
private void generateTitleView()
{
if (getChildCount() > 0)
this.removeAllViews();
count = titles.length;
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(count*tabWidth, LinearLayout.LayoutParams.MATCH_PARENT));
for (int i = 0; i < count; i++)
{
TextView tv = new TextView(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(tabWidth,
LinearLayout.LayoutParams.MATCH_PARENT);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(COLOR_TEXT_NORMAL);
tv.setText(titles[i]);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);//字体大小
tv.setLayoutParams(lp);
final int finalI = i;
tv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(viewPager!=null){
viewPager.setCurrentItem(finalI,false);
}
}
});
linearLayout.addView(tv);
}
addView(linearLayout);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
scroll(position, positionOffset);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
}