19号下午4点 老板突然觉得导航条不好看, 要做成和别的app一样。 然而20号要上线 真是醉人,加班做了个 HorizontalScrollView。 今天重新封装成控件 。 增加动态 改变文字
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyHorizontalScrollView">
<attr name="textSize" format="integer"/>
<attr name="leftPadding" format="integer"></attr>
<attr name="rightPadding" format="integer"></attr>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:wk="http://schemas.android.com/apk/res/com.example.x6.myhorizontalscrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.x6.myhorizontalscrollview.MainActivity">
<com.example.x6.myhorizontalscrollview.MyHorizontalScrollView
android:id="@+id/hol"
android:layout_width="match_parent"
android:layout_height="40dp"
wk:textSize="15"
wk:leftPadding="10"
wk:rightPadding="10"
></com.example.x6.myhorizontalscrollview.MyHorizontalScrollView>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private String[] data = {"哈","哈1","哈11","哈123","哈1234","哈5342"
,"哈沙发","哈案案发的方式","哈问问特如果","哈",};
private String[] data2 = {"改变1","改变123","改变地方","改变45区v","改变234地方","改变"
,"改变","改变2热情为让发的是犯法","改变","改变",};
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
hol.addTag(data);
}
};
private MyHorizontalScrollView hol;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hol = (MyHorizontalScrollView) findViewById(R.id.hol);
btn = (Button) findViewById(R.id.btn);
//网络获取数据 延时 1S
handler.sendEmptyMessageDelayed(0,1000);
hol.setListener(new MyHorizontalScrollView.OnTouchLetterChangedListener() {
@Override
public void onTouchLetterChanged(int index) {
Toast.makeText(MainActivity.this,"当前点击:"+index,Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hol.addTag(data2);
}
});
}
}
public class MyHorizontalScrollView extends HorizontalScrollView {
private Context context;
private LinearLayout linLayout;
private int width;
private int height;
private int textSize;
private int leftPadding;
private int rightPadding;
private List<Integer> moveXList = new ArrayList<>();
private List<TextView> tvList = new ArrayList<>();
public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init(context,attrs);
}
public MyHorizontalScrollView(Context context) {
super(context);
}
private void init(Context context,AttributeSet attrs) {
TypedArray ta= context.obtainStyledAttributes(attrs,R.styleable.MyHorizontalScrollView);
textSize = ta.getInteger(R.styleable.MyHorizontalScrollView_textSize,15);
leftPadding = ta.getInteger(R.styleable.MyHorizontalScrollView_leftPadding,15);
rightPadding = ta.getInteger(R.styleable.MyHorizontalScrollView_rightPadding,15);
ta.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
initChildX();
}
//计算 textview的宽度
private void initChildX(){
if(getChildCount()>0){
ViewGroup view = (ViewGroup) getChildAt(0);
if(view.getChildCount()>0){
moveXList.clear();
//第一个数据 无需移动
moveXList.add(0);
int moveX = 0;
for(int i=0; i<view.getChildCount()-1; i++){
moveX += view.getChildAt(i).getMeasuredWidth();
moveXList.add(moveX);
}
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
addLinLayout(context);
super.onLayout(changed, l, t, r, b);
}
private void addLinLayout(Context context) {
if(this.getChildCount()==1)return ;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
width,height);
linLayout = new LinearLayout(context);
linLayout.setGravity(LinearLayout.HORIZONTAL);
this.addView(linLayout,layoutParams);
}
public void addTag(String[] data) {
if(linLayout!=null)
linLayout.removeAllViews();
for(int i=0; i<data.length; i++){
LinearLayout.LayoutParams titleLayout = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
height);
TextView tv = new TextView(context);
if(i == 0){
tv.setTextColor(0xff46abff);
}else{
tv.setTextColor(0xff333333);
}
tv.setText(data[i]);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
tv.setGravity(Gravity.VERTICAL_GRAVITY_MASK);
tv.setPadding(dip2px(context,leftPadding), 0, dip2px(context,rightPadding), 0);
final int index = i;
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
smoothScrollTo(moveXList.get(index),0);
for(int n=0; n<tvList.size(); n++){
tvList.get(n).setTextColor(0xFF333333);
}
((TextView) v).setTextColor(0xFF46ABFF);
listener.onTouchLetterChanged(index);
}
});
tvList.add(tv);
if(linLayout!=null)
linLayout.addView(tv,titleLayout);
}
scrollTo(0,0);
this.invalidate();
}
private int dip2px(Context context, float dpValue) {
if (context != null) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
} else {
return 0;
}
}
private OnTouchLetterChangedListener listener;
public OnTouchLetterChangedListener getListener() {
return listener;
}
public void setListener(OnTouchLetterChangedListener listener) {
this.listener = listener;
}
public interface OnTouchLetterChangedListener{
public void onTouchLetterChanged(int index);
}
}