Java tutorial
package ecust.news; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.TextView; import utils.Global; /** * ============================================================================= * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * as published by the Free Software Foundation. * . * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * . * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * ============================================================================= * . * Created by on 2015/9/4 * Copyright (C) 2015 * ?https://github.com/chenjj2048 * . */ //ViewPageIndicator,?? //??TabPageIndicator? public class myWidgetTabPageIndicator extends HorizontalScrollView implements View.OnClickListener, ViewPager.OnPageChangeListener { // final int textViewFocusedColor = Color.WHITE; // final int textViewUnfocusedColor = Color.argb(180, 255, 255, 255); // private final int dp_FocusedLineHeight = 5; //?ViewPager private ViewPager viewPager; // private LinearLayout linearLayout; // private ViewPager.OnPageChangeListener listener; // private Paint paint = new Paint(); public myWidgetTabPageIndicator(Context context) { super(context); init(); } public myWidgetTabPageIndicator(Context context, AttributeSet attrs) { super(context, attrs); init(); } public myWidgetTabPageIndicator(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { this.setHorizontalScrollBarEnabled(false); linearLayout = new LinearLayout(getContext()); linearLayout.setOrientation(LinearLayout.HORIZONTAL); this.addView(linearLayout); } public void setViewPager(ViewPager viewPager) { this.viewPager = viewPager; viewPager.setOnPageChangeListener(this); notifyDataSetChanged(); } public void notifyDataSetChanged() { PagerAdapter pagerAdapter = this.viewPager.getAdapter(); linearLayout.removeAllViews(); for (int i = 0; i < pagerAdapter.getCount(); i++) { //? String title = pagerAdapter.getPageTitle(i).toString(); //TextView addTab(i, title); } } @Override public void onClick(View v) { if (!(v instanceof TextView)) return; TextView currentView = (TextView) v; //position??Tag? int position; for (position = 0; position < linearLayout.getChildCount(); position++) { TextView textView = (TextView) linearLayout.getChildAt(position); //? if (textView.getText().toString().equals(currentView.getText().toString())) break; } // if (position >= linearLayout.getChildCount()) return; //?? viewPager.setCurrentItem(position, true); } //? private void addTab(int i, String title) { TextView textView = new TextView(getContext()); textView.setText(title); //? textView = decorateTextView(textView); if (i == 0) textView.setTextColor(textViewFocusedColor); textView.setOnClickListener(this); //View linearLayout.addView(textView); } //TextView public TextView decorateTextView(TextView textView) { textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Global.dimenConvert.dip2px(20)); textView.setSingleLine(); textView.setTextColor(textViewUnfocusedColor); textView.setFocusable(true); textView.setClickable(true); //Padding final int paddingLeftRight = Global.dimenConvert.dip2px(12); final int paddingTop = Global.dimenConvert.dip2px(8); final int paddingBottom = Global.dimenConvert.dip2px(0); textView.setPadding(paddingLeftRight, paddingTop, paddingLeftRight, paddingBottom); return textView; } //? public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) { this.listener = listener; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // int w = Global.dimenConvert.dip2px(dp_FocusedLineHeight) / 2; int y = canvas.getHeight() - w / 2; paint.setColor(Color.WHITE); paint.setStrokeWidth(w); canvas.drawLine(0, y, canvas.getMaximumBitmapWidth(), y, paint); } //? public void setCurrentItem(int position) { // for (int i = 0; i < linearLayout.getChildCount(); i++) { TextView textView = (TextView) linearLayout.getChildAt(i); if (i != position) { textView.setTextColor(textViewUnfocusedColor); textView.setCompoundDrawables(null, null, null, null); } else { textView.setTextColor(textViewFocusedColor); //drawable textView.measure(0, 0); int w = textView.getMeasuredWidth() - textView.getPaddingLeft() - textView.getPaddingRight(); int h = Global.dimenConvert.dip2px(dp_FocusedLineHeight); Drawable drawable = new ColorDrawable(Color.WHITE); drawable.setBounds(0, 0, w, h); textView.setCompoundDrawables(null, null, null, drawable); } } //?textView?? int sum_width = 0; for (int i = 0; i < position; i++) { sum_width += linearLayout.getChildAt(i).getWidth(); } //? sum_width -= this.getWidth() / 2 - linearLayout.getChildAt(position).getWidth() / 2; // this.smoothScrollTo(sum_width, 0); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (listener != null) listener.onPageScrolled(position, positionOffset, positionOffsetPixels); } @Override public void onPageSelected(int position) { //? setCurrentItem(position); if (listener != null) listener.onPageSelected(position); } @Override public void onPageScrollStateChanged(int state) { if (listener != null) listener.onPageScrollStateChanged(state); } }