MainActivity
public class MainActivity extends AppCompatActivity {
private AddSubtractView mAsv;
private TrapezoidView mTv;
private ViewGroup.MarginLayoutParams layoutParams;
private int width;
private int height;
private int num = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取屏幕的宽度
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
initView();
initData();
}
private void initData() {
mTv = (TrapezoidView) findViewById(R.id.tv);
layoutParams = new ViewGroup.MarginLayoutParams(width / 3, 50);
layoutParams.leftMargin = 5;
layoutParams.rightMargin = 5;
layoutParams.topMargin = 5;
layoutParams.bottomMargin = 5;
for (int i=0; i< num; i++){
TextView textView = new TextView(this);
textView.setText(i + 1 + "");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.BLACK);
mTv.addView(textView, layoutParams);
}
mTv.setOnItemClickListener(new TrapezoidView.OnItemClickListener() {
@Override
public void onItemClick(View view) {
TextView tv = (TextView) view;
String string = tv.getText().toString();
Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,ShowDataActivity.class);
intent.putExtra("page",string);
startActivity(intent);
}
@Override
public void onItemLongClick(View view) {
mTv.deleteItem(view);
}
});
}
private void initView() {
mAsv = (AddSubtractView) findViewById(R.id.asv);
mAsv.setmOnAddSubtractListener(new AddSubtractView.OnAddSubtractListener() {
@Override
public void onClickAdd(View view) {
num++;
TextView textView = mTv.addItem(num);
mTv.addView(textView, layoutParams);
}
});
}
}
TrapezoidView
public class TrapezoidView extends ViewGroup {
private View childAt;
private int startWidth;
private int startHeight;
public TrapezoidView(Context context) {
this(context , null);
}
public TrapezoidView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TrapezoidView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams params = null;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
if (getChildCount() == 0){
setMeasuredDimension(0 ,0);
}else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST){
View childOne = getChildAt(0);
params = (MarginLayoutParams) childOne.getLayoutParams();
int childWidth = childOne.getMeasuredWidth();
int childHeigth = childOne.getMeasuredHeight();
setMeasuredDimension(childWidth + params.leftMargin + params.rightMargin,
childHeigth * getChildCount() + params.topMargin + params.bottomMargin);
}else if (widthMode == MeasureSpec.AT_MOST){
View childOne = getChildAt(0);
params = (MarginLayoutParams) childOne.getLayoutParams();
int childWidth = childOne.getMeasuredWidth();
setMeasuredDimension(childWidth + params.leftMargin + params.rightMargin, heightSize);
}else if (heightMode == MeasureSpec.AT_MOST){
View childOne = getChildAt(0);
params = (MarginLayoutParams) childOne.getLayoutParams();
int childHeight = childOne.getMeasuredHeight();
setMeasuredDimension(widthSize, childHeight * getChildCount() + params.topMargin + params.bottomMargin);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
startWidth = 0;
startHeight = 0;
int width = getWidth();
int childCount = getChildCount();
for (int i=0; i<childCount; i++){
childAt = getChildAt(i);
if (i%3 == 0){
childAt.setBackgroundColor(Color.RED);
}
if (i%3 == 1){
childAt.setBackgroundColor(Color.GREEN);
}
if (i%3 == 2){
childAt.setBackgroundColor(Color.BLUE);
}
childAt.layout(startWidth, startHeight, startWidth + childAt.getMeasuredWidth(), startHeight + childAt.getMeasuredHeight());
startWidth += childAt.getMeasuredWidth();
startHeight += childAt.getMeasuredHeight();
if (startWidth >= width){
startWidth = 0;
}
}
childAt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mOnItemClickListener != null){
mOnItemClickListener.onItemClick(view);
}
}
});
childAt.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (mOnItemClickListener != null){
mOnItemClickListener.onItemLongClick(view);
}
return true;
}
});
}
public TextView addItem(int num){
TextView textView = new TextView(getContext());
textView.setText(num +"");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.BLACK);
setAnim(textView);
return textView;
}
private void setAnim(TextView textView){
ObjectAnimator translationY = ObjectAnimator.ofFloat(textView, "translationY", new float[]{0f, startHeight, 0});
ObjectAnimator translationX = ObjectAnimator.ofFloat(textView, "translationX", new float[]{0f, startWidth, 0});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(translationY, translationX);
animatorSet.setDuration(5000);
animatorSet.start();
}
//删除条目
public void deleteItem(View view) {
removeView(view);
}
public interface OnItemClickListener{
void onItemClick(View view);
void onItemLongClick(View view);
}
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener listener){
this.mOnItemClickListener = listener;
}
}
AddSubtractView
public class AddSubtractView extends LinearLayout {
private TextView tv_num;
public AddSubtractView(Context context) {
this(context, null);
}
public AddSubtractView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubtractView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
View inflate = View.inflate(context, R.layout.add_subtract, this);
TextView tv_add = (TextView) inflate.findViewById(R.id.add);
tv_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mOnAddSubtractListener != null){
mOnAddSubtractListener.onClickAdd(view);
}
}
});
}
//设置接口
public interface OnAddSubtractListener{
void onClickAdd(View view);
}
//声明接口
private OnAddSubtractListener mOnAddSubtractListener;
//提供方法
public void setmOnAddSubtractListener(OnAddSubtractListener listener){
this.mOnAddSubtractListener = listener;
}
}
main_layout
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.com.yuekao_moni2.MainActivity">
<com.example.com.yuekao_moni2.view.AddSubtractView
android:id="@+id/asv"
android:layout_width="match_parent"
android:layout_height="50dp"></com.example.com.yuekao_moni2.view.AddSubtractView>
<com.example.com.yuekao_moni2.view.TrapezoidView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.com.yuekao_moni2.view.TrapezoidView>
</LinearLayout>
add_subtract
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/num"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:text="三色梯"
android:textSize="20dp" />
<TextView
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="+"
android:textSize="20dp" />
</LinearLayout>