1.
implementation 'com.github.bumptech.glide:glide:4.7.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1' implementation 'com.jcodecraeer:xrecyclerview:1.5.9' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.google.code.gson:gson:2.8.5'
2.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
3.
<Button android:id="@+id/add" android:onClick="add" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.example.thinkpad.myapplication.ThreeColorView android:id="@+id/threecolorview" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.example.thinkpad.myapplication.ThreeColorView>
4.
package com.example.thinkpad.myapplication; import android.content.Context; import android.content.Intent; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; public class ThreeColorView extends ViewGroup { public ThreeColorView(Context context) { super(context); } public ThreeColorView(Context context, AttributeSet attrs) { super(context, attrs); } public ThreeColorView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 把此view的最终的宽度和高度定下来 * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int totalHeight = 0;//此控件的高度 int totalWidth = 0;//此控件的宽度 //得到子view数量 int child = getChildCount(); if (child > 0) { for (int i = 0; i < child; i++) {//遍历子控件 View view = getChildAt(i);//得到此容器所有的子view totalHeight += view.getMeasuredHeight(); measureChild(view,widthMeasureSpec,heightMeasureSpec); // view.measure(widthMeasureSpec, heightMeasureSpec); } } totalWidth = AppUtil.screenWidth(getContext()); System.out.println("width:"+totalWidth); System.out.println("height:"+totalHeight); //设置宽度和高度给当前view,通过下面这个方法 setMeasuredDimension(totalWidth, totalHeight); } @Override protected void onLayout(boolean bo, int left, int top, int right, int bottom) { int l = 0; int t = 0; int r = 0; int b = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View view = getChildAt(i);//得到每一个view的对象 view.layout(l, t, l + view.getMeasuredWidth(), t + view.getMeasuredHeight()); l += view.getMeasuredWidth(); System.out.println("llll:"+l); t += view.getMeasuredHeight(); if (l+view.getMeasuredWidth()>AppUtil.screenWidth(getContext())){ l = 0; } // if (AppUtil.screenWidth(getContext()) - l < view.getMeasuredWidth()) { // l = 0; // } //点击事件 final int finalI = i; view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getContext(), finalI +":点击位置", Toast.LENGTH_SHORT).show(); TextView textView = (TextView) view; Toast.makeText(getContext(), textView.getText().toString() +"文本", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getContext(), MainActivity.class); intent.putExtra("id",textView.getText().toString()); getContext().startActivity(intent); } }); view.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View view) { Toast.makeText(getContext(), finalI +":长按位置", Toast.LENGTH_SHORT).show(); removeView(view); return true; } }); } } }
5.
package com.example.thinkpad.myapplication; import android.content.Context; import android.util.DisplayMetrics; public class AppUtil { public static int screenWidth(Context context){ DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return metrics.widthPixels; } }
6.
package com.example.thinkpad.myapplication; import android.animation.ObjectAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Main2Activity extends AppCompatActivity { private ThreeColorView threeColorView; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); threeColorView = findViewById(R.id.threecolorview); } public void add(View view) { count++; int width = AppUtil.screenWidth(this); TextView textView = new TextView(this); textView.setText(count+""); textView.setGravity(Gravity.CENTER); textView.setTextColor(getResources().getColor(R.color.colorAccent)); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationX",(width-width/3),0); objectAnimator.setDuration(3000); objectAnimator.start(); if (count==1||count==4||count==7){ textView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); }else{ textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); } threeColorView.addView(textView); //得到view的属性参数 ViewGroup.LayoutParams params = textView.getLayoutParams(); params.width = width/3; params.height = 70; textView.setLayoutParams(params); } }