import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.houdask.judicature.exam.R;
import java.util.ArrayList;
/**
* @deprecated : 一个不与任何控件进行绑定的可以水平滑动的 TabLayout,如果想绑定 ViewPager 或其他控件,
* 请自行实现监听事件进行控制
*/
public class FreedomSlidingTabLayout extends HorizontalScrollView
implements View.OnClickListener, View.OnTouchListener {
/**
* 滚动状态:
* IDLE=滚动停止
* TOUCH_SCROLL=手指拖动滚动
* FLING=滚动
*/
public enum ScrollType {IDLE, TOUCH_SCROLL, FLING}
private Handler mHandler;
/**
* 记录当前滚动的距离
*/
private int currentX = -9999999;
/**
* 当前滚动状态
*/
private ScrollType scrollType = ScrollType.IDLE;
private OnScrollChangeListener onScrollChangeListener;
/**
* 滚动监听间隔:milliseconds
* 时间越小灵敏度越高
*/
public int scrollMonitorInterval = 20;
/**
* 滚动监听runnable
*/
private Runnable scrollRunnable = new Runnable() {
@Override
public void run() {
if (getScrollX() == currentX) {
//滚动停止,取消监听线程
scrollType = ScrollType.IDLE;
if (onScrollChangeListener != null) {
onScrollChangeListener.onScrollChanged(scrollType);
}
mHandler.removeCallbacks(this);
return;
} else {
//手指离开屏幕,但是view还在滚动
scrollType = ScrollType.FLING;
if (onScrollChangeListener != null) {
onScrollChangeListener.onScrollChanged(scrollType);
}
}
currentX = getScrollX();
//mHandler.postDelayed(this, 10);
try {
Thread.sleep(scrollMonitorInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
run();
}
};
private Context context;
private ArrayList<String> titles = new ArrayList<>();
private LinearLayout viewGroup;
/**
* 上一次被选中的Item
*/
private int lastPosition = -1;
private int currentPosition = -1;
public FreedomSlidingTabLayout(Context context) {
super(context);
this.context = context;
init();
}
public FreedomSlidingTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.FreedomSlidingTabLayout);
itemSelectTextColor =
array.getColor(R.styleable.FreedomSlidingTabLayout_fs_itemSelectTextColor,
Color.BLACK);
itemTextColor = array.getColor(R.styleable.FreedomSlidingTabLayout_fs_itemTextColor,
Color.BLACK);
itemMargins = array.getDimension(R.styleable.FreedomSlidingTabLayout_fs_itemMargins, 0);
itemBackGround = array.getColor(R.styleable.FreedomSlidingTabLayout_fs_itemBackGround,
Color.WHITE);
// itemTextStyle = array.gete(R.styleable.FreedomSlidingTabLayout_fs_itemTextStyle,
// TextStyle.NARROW);
itemTextSize = array.getDimension(R.styleable.FreedomSlidingTabLayout_fs_itemTextSize,
R.dimen.sixteen_sp);
this.context = context;
init();
}
private int itemSelectTextColor;
private int itemTextColor;
private float itemMargins;
private int itemBackGround;
private int itemTextStyle;
private float itemTextSize;
private void setItemSelectChange(int position) {
if (currentPosition == position) {
return;
}
lastPosition = currentPosition;
currentPosition = position;
TextView currentView = (TextView) viewGroup.getChildAt(currentPosition);
currentView.setTextColor(itemSelectTextColor);
TextView lastView = (TextView) viewGroup.getChildAt(lastPosition);
lastView.setTextColor(itemTextColor);
}
public FreedomSlidingTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.FreedomSlidingTabLayout);
itemSelectTextColor =
array.getColor(R.styleable.FreedomSlidingTabLayout_fs_itemSelectTextColor,
Color.BLACK);
itemTextColor = array.getColor(R.styleable.FreedomSlidingTabLayout_fs_itemTextColor,
Color.BLACK);
itemMargins = array.getDimension(R.styleable.FreedomSlidingTabLayout_fs_itemMargins, 0);
itemBackGround = array.getColor(R.styleable.FreedomSlidingTabLayout_fs_itemBackGround,
Color.WHITE);
// itemTextStyle = array.gete(R.styleable.FreedomSlidingTabLayout_fs_itemTextStyle,
// TextStyle.NARROW);
itemTextSize = array.getDimension(R.styleable.FreedomSlidingTabLayout_fs_itemTextSize,
R.dimen.sixteen_sp);
this.context = context;
init();
}
private void init() {
setting();
initViewGroup();
addView(viewGroup);
}
/**
* 初始化 viewGroup(LinearLayout)
*/
private void initViewGroup() {
viewGroup = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
viewGroup.setOrientation(LinearLayout.HORIZONTAL);
viewGroup.setLayoutParams(layoutParams);
}
/**
* 清空 @{viewGroup(LinearLayout)},重新创建 Item 并填充到 viewGroup 中
*/
private void initChildView() {
if (titles != null && titles.size() > 0) {
viewGroup.removeAllViews();
for (int i = 0; i < titles.size(); i++) {
TextView textView = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (i != 0) {
layoutParams.setMargins((int) itemMargins, 0, 0, 0);
}
textView.setGravity(Gravity.CENTER);
textView.setText(titles.get(i));
// textView.setTypeface(Typeface.defaultFromStyle(itemTextStyle));
textView.setTag(i);
/*此处如果直接使用 textView.setTextSize() 设置文字大小会导致文字大于正常值*/
textView.getPaint().setTextSize(itemTextSize);
if (i == 0) {
if (currentPosition < 0) {
currentPosition = 0;
/* 如果与其他控件进行关联,第一次初始化内容是可能导致无法与其他控件联动,
* 所以在此处进行判断,如果 currentPosition < 0 (代表是第一次初始化,
* 没有进行点击操作),回调一次点击事件。*/
if (onScrollChangeListener != null) {
onScrollChangeListener.onItemSelset(textView, currentPosition);
}
}
textView.setTextColor(itemSelectTextColor);
} else {
textView.setTextColor(itemTextColor);
}
textView.setClickable(true);
textView.setOnClickListener(this);
textView.setLayoutParams(layoutParams);
viewGroup.addView(textView);
}
}
}
/**
* HorizontalScrollView 属性设置
*/
private void setting() {
mHandler = new Handler();
setHorizontalScrollBarEnabled(false);
setOnTouchListener(this);
}
/**
* 将当前控件滑动到父控件正中位置(可以的话)
*/
private void setViewToCenter(View view) {
int scrollViewWidth = this.getWidth();
/*当前Item的左侧坐标位置加上Item宽度的一半,这个长度相当于当前Item的中心位置距离父控件左侧的距离*/
int rb_px = (int) view.getX() + view.getWidth() / 2;
/*当前Item中心位置减去父控件中心位置等于当前Item距正中的距离*/
this.smoothScrollTo(rb_px - scrollViewWidth / 2, 0);
}
/**
* 清除原有数据(如果有),重新设置数据源,并开始绘制TabLayout
* 每次调用该方法都会重新绘制一遍所有控件
*/
public void setTabData(ArrayList<String> titles) {
this.titles.clear();
this.titles = new ArrayList<>();
this.titles.addAll(titles);
this.post(new Runnable() {
@Override
public void run() {
initChildView();
}
});
}
/**
* 将选定位置的Item移动到父控件正中位置
*/
public void setCurrentItem(final int position) {
/*防止在Item还没有绘制完就调用当前方法的状况*/
viewGroup.post(new Runnable() {
@Override
public void run() {
View view = viewGroup.getChildAt(position);
setViewToCenter(view);
setItemSelectChange(position);
if (onScrollChangeListener != null) {
mHandler.post(scrollRunnable);
onScrollChangeListener.onItemSelset(view, currentPosition);
}
}
});
}
/**
* 滑动与点击事件的监听
*/
public void setScrollMonitorInterval(int scrollMonitorInterval) {
this.scrollMonitorInterval = scrollMonitorInterval;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
this.scrollType = ScrollType.TOUCH_SCROLL;
onScrollChangeListener.onScrollChanged(scrollType);
mHandler.removeCallbacks(scrollRunnable);
break;
case MotionEvent.ACTION_UP:
mHandler.post(scrollRunnable);
break;
}
return super.onTouchEvent(event);
}
@Override
public void onClick(View view) {
setViewToCenter(view);
setItemSelectChange((int) view.getTag());
if (onScrollChangeListener != null) {
mHandler.post(scrollRunnable);
onScrollChangeListener.onItemSelset(view, currentPosition);
}
}
/**
* 设置滚动监听间隔:milliseconds
* 时间越小灵敏度越高
*/
public void setOnScrollChangeListener(OnScrollChangeListener listener) {
onScrollChangeListener = listener;
}
public interface OnScrollChangeListener {
void onItemSelset(View view, int position);
void onScrollChanged(ScrollType scrollType);
}
}
<declare-styleable name="FreedomSlidingTabLayout">
<attr name="fs_itemSelectTextColor" format="color" />
<attr name="fs_itemTextColor" format="color" />
<attr name="fs_itemMargins" format="dimension" />
<attr name="fs_itemBackGround" format="color" />
<attr name="fs_itemTextStyle" format="enum" />
<attr name="fs_itemTextSize" format="dimension" />
</declare-styleable>
|