自定义ViewGroup三色梯

LadderView

public class LadderView extends ViewGroup{
    public LadderView(Context context) {
        super(context);
    }

    public LadderView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LadderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //此控件高度
        int totalHeight=0;
        //此控件宽度
        int totalWidth=0;
        //子View数量
        int childCount = getChildCount();
        if (childCount>0){
            //遍历子控件
            for (int i = 0; i < childCount; i++) {
                //得到此容器所有子View
                View view=getChildAt(i);
               //屏幕高度
                totalHeight+=view.getMeasuredHeight();
                measureChild(view,widthMeasureSpec,heightMeasureSpec);
            }
        }
        //屏幕宽度
        totalWidth=WidthUtils.ScreenWidth(getContext());
        setMeasuredDimension(totalWidth,totalHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left=0;
int top=0;
int right=0;
int bottom=0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View view = getChildAt(i);
            view.layout(left,top,left+view.getMeasuredWidth(),top+view.getMeasuredHeight());
            left+=view.getMeasuredWidth();
            top+=view.getMeasuredHeight();
            if (left+view.getMeasuredWidth()>WidthUtils.ScreenWidth(getContext())){
                left=0;
            }
            final int I=i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(),"第"+I+"条",Toast.LENGTH_SHORT).show();
                    TextView textView= (TextView) view;
                    Intent intent=new Intent(getContext(), MainActivity.class);
                    String string = textView.getText().toString();
                    intent.putExtra("id",string);
                    getContext().startActivity(intent);
                }
            });
            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                   removeView(view);
                   Toast.makeText(getContext(),"删除第"+I+"条",Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
        }
    }
}

LadderActivity

public class LadderActivity extends AppCompatActivity {

    private LadderView lv;
    private Button bt;
    private int count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ladder);
        lv = findViewById(R.id.lv);
        bt = findViewById(R.id.bt);
    }

    public void add(View view) {
        count++;
        int width = WidthUtils.ScreenWidth(this);
        TextView tv=new TextView(this);
        tv.setText(count+"");
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(getResources().getColor(R.color.colorWhite));
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(tv,"translationX",(width-width/3),0);
        objectAnimator.setDuration(1000);
        objectAnimator.start();
        if (count==1||count==4||count==7||count==10||count==13||count==16||count==19){
            tv.setBackgroundColor(getResources().getColor(R.color.colorAccent));

        }
        else if(count==2||count==5||count==8||count==11||count==14||count==17||count==20){
            tv.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

        }
        else if (count==3||count==6||count==9||count==12||count==15||count==18||count==21){
            tv.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

        }
        lv.addView(tv);
        ViewGroup.LayoutParams layoutParams = tv.getLayoutParams();
        layoutParams.width=width/3;
        layoutParams.height=70;
        tv.setLayoutParams(layoutParams);
    }

    public void sub(View view) {
        count--;
        int childcount = lv.getChildCount();

            lv.removeViewAt(childcount - 1);
        }
    }
 

WdithUtils

package com.example.test_demo.widgt;

import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Created by john on 2018/5/30.
 */

public class WidthUtils {
    /**
     * 屏幕宽度
     */
    public static int ScreenWidth(Context context){
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels;
    }
}

activity_ladder



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.test_demo.LadderActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sub"
        android:text="-"
        android:textSize="20dp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/bt"
    android:onClick="add"
    android:text="+"
    android:textSize="20dp"/>
    </LinearLayout>
    <com.example.test_demo.widgt.LadderView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv"></com.example.test_demo.widgt.LadderView>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值