My investment 3-hot financial management, investment 3-hot Financial Management
Implementation of stream layout of "Popular Financial Management"
1. Custom stream Layout
Public class FlowLayout extends ViewGroup {public FlowLayout (Context context) {super (context);} public FlowLayout (Context context, AttributeSet attrs) {super (context, attrs );} public FlowLayout (Context context, AttributeSet attrs, int values) {super (context, attrs, region) ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int widthMode = MeasureS Pec. getMode (widthMeasureSpec); int widthSize = MeasureSpec. getSize (widthMeasureSpec); int heightMode = MeasureSpec. getMode (heightMeasureSpec); int heightSize = MeasureSpec. getSize (heightMeasureSpec); // record the width and height of the current layout. int width = 0; int height = 0; // record the width and height of a row of views. int lineWidth = 0; int lineHeight = 0; // obtain the width and height of the layout in at_most mode. int childCount = getChildCount (); for (int I = 0; I <childCount; I ++) {View ChildView = getChildAt (I); measureChild (childView, widthMeasureSpec, heightMeasureSpec); int childWidth = childView. getMeasuredWidth (); int childHeight = childView. getMeasuredHeight (); MarginLayoutParams mp = (MarginLayoutParams) childView. getLayoutParams (); // indicates that the current sub-view can be placed on a row if (lineWidth + childWidth + mp. leftMargin + mp. rightMargin <widthSize) {lineWidth + = childWidth + mp. leftMargin + mp. RightMargin; lineHeight = Math. max (lineHeight, childHeight + mp. topMargin + mp. bottomMargin);} else {// wrap width = Math. max (width, lineWidth); height + = lineHeight; // reset lineWidth = childWidth + mp. leftMargin + mp. rightMargin; lineHeight = childHeight + mp. topMargin + mp. bottomMargin;} if (I = childCount-1) {width = Math. max (width, lineWidth); height + = lineHeight;} Log. e ("TAG", "width =" + Width + ", height =" + height); setMeasuredDimension (widthMode = MeasureSpec. EXACTLY? WidthSize: width, heightMode = MeasureSpec. EXACTLY? HeightSize: height);} private List <View> allViews = new ArrayList <List <View> (); private List <Integer> allHeights = new ArrayList <> (); @ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {int width = getWidth (); int childCount = getChildCount (); int lineWidth = 0; int lineHeight = 0; List <View> lineViews = new ArrayList <> (); for (int I = 0; I <childCount; I ++) {View childView = getChildAt (I); int childWidth = childView. getMeasuredWidth (); int childHeight = childView. getMeasuredHeight (); MarginLayoutParams mp = (MarginLayoutParams) childView. getLayoutParams (); // if (lineWidth + childWidth + mp. leftMargin + mp. rightMargin <width) {lineWidth + = childWidth + mp. leftMargin + mp. rightMargin; lineViews. add (childView); lineHeight = Math. max (lineHeight, childHeight + mp. topMargin + mp. bottomMargin);} else {// wrap allViews. add (lineViews); allHeights. add (lineHeight); lineViews = new ArrayList <> (); lineViews. add (childView); lineWidth = childWidth + mp. leftMargin + mp. rightMargin; lineHeight = childHeight + mp. topMargin + mp. bottomMargin;} if (I = childCount-1) {allViews. add (lineViews); allHeights. add (lineHeight) ;}} Log. e ("TAG", "allViews. size = "+ allViews. size () + ", allHeights. size = "+ allHeights. size (); // calculate the left, top, right, and bottom values of each sub-view in each row. The position of each sub-view in each row is int x = 0; int y = 0; for (int I = 0; I <allViews. size (); I ++) {int curLineHeight = allHeights. get (I); // all views of the current row <view> views = allViews. get (I); for (View view: views) {int viewWidth = view. getMeasuredWidth (); int viewHeight = view. getMeasuredHeight (); MarginLayoutParams mp = (MarginLayoutParams) view. getLayoutParams (); int lc = x + mp. leftMargin; int tc = y + mp. topMargin; int rc = lc + viewWidth; int bc = tc + viewHeight; view. layout (lc, tc, rc, bc); x + = viewWidth + mp. rightMargin + mp. leftMargin;} x = 0; y + = curLineHeight;} // with the following method, you can use child to getLayoutParams () @ Override public ViewGroup. layoutParams generateLayoutParams (AttributeSet attrs) {MarginLayoutParams mp = new MarginLayoutParams (getContext (), attrs); return mp ;}}
2. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.atguigu.p2p.ui.FlowLayout android:id="@+id/flow_hot" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_green_light"> </com.atguigu.p2p.ui.FlowLayout></LinearLayout>
3. ProductHotFragment
Public class ProductHotFragment extends BaseFragment {@ Bind (R. id. flow_layout) FlowLayout flowLayout; // provide the private String [] datas = new String [] {"Newbie Plan", "90-day plan of the happy live series ", "wallet", "30-day financial planning (raising interest rates by 2%)", "Forestry Bureau investing in commercial operations and making a profit", "purchasing vehicles by Middle School Teachers", "qisi Xihai business planning ", "New xiyou film shooting", "Java Training Instructor turnover", "HelloWorld", "C ++-C-ObjectC-java", "Android vs ios ", "algorithms and data structures", "JNI and NDK", "team working"}; private Random random; @ Override protected RequestParams getParams () {return null ;} @ Override protected String getUrl () {return null ;}@ Override protected void initData (String content) {random = new Random (); // 1. dynamically create TextView for (int I = 0; I <datas. length; I ++) {final TextView TV = new TextView (getActivity (); // sets the TextView attribute TV. setText (datas [I]); TV. setTextSize (UIUtils. dp2Px (10); ViewGroup. marginLayoutParams mp = new ViewGroup. marginLayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); mp. leftMargin = UIUtils. dp2Px (8); mp. rightMargin = UIUtils. dp2Px (8); mp. topMargin = UIUtils. dp2Px (8); mp. bottomMargin = UIUtils. dp2Px (8); TV. setLayoutParams (mp); // sets the textView background int red = random. nextInt (211); int green = random. nextInt (211); int blue = random. nextInt (211); // Test 1: // TV. setBackground (DrawUtils. getDrawable (Color. rgb (red, green, blue), UIUtils. dp2Px (5); // Test 2: TV. setBackground (DrawUtils. getSelector (DrawUtils. getDrawable (Color. rgb (red, green, blue), UIUtils. dp2Px (5), DrawUtils. getDrawable (Color. WHITE, UIUtils. dp2Px (5); // save and press to display the selector effect. You need to set the following attribute // TV. setClickable (true); // Add a click event, which is also a way to display the selector effect. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (ProductHotFragment. this. getActivity (), TV. getText (), Toast. LENGTH_SHORT ). show () ;}}); // set the inner margin int padding = UIUtils. dp2Px (5); TV. setPadding (padding, padding); // 2. add to FlowLayout layout. addView (TV) ;}@ Override protected void initTitle () {}@ Override public int getLayoutId () {return R. layout. fragment_product_hot ;}}