preface

Recently, the company has a label set feature that allows users to filter labels to generate a series of actions that funnel the selection and ultimately recommend drugs

Let’s cut the crap and see if it’s what we want

Implementation effect

Implementation code AutoNextLineLinearLayout

Many people will encounter a full line automatically switch to the next line of interface requirements, and the Android built-in LinearLayout can be horizontal or vertical, not enough to display the ScrollView, horizontal and vertical mixed row is not good.

Public class AutoNextLineLinearLayout extends LinearLayout {int mLeft, mRight, mTop, mBottom; Hashtable map = new Hashtable(); public AutoNextLineLinearLayout(Context context) { super(context); } public AutoNextLineLinearLayout(Context context, int horizontalSpacing, int verticalSpacing) { super(context); } public AutoNextLineLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int mWidth = MeasureSpec.getSize(widthMeasureSpec); int mCount = getChildCount(); int mX = 0; int mY = 0; mLeft = 0; mRight = 0; mTop = 5; mBottom = 0; int j = 0; for (int i = 0; i < mCount; i++) { final View child = getChildAt(i); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) child.getLayoutParams(); child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); Int childw = child.getMeasuredWidth()+params.leftMargin + params.rightMargin; int childw = child.getMeasuredWidth()+params.leftMargin + params.rightMargin; int childh = child.getMeasuredHeight(); mX += childw; Position Position = new Position(); Position Position = new Position(); mLeft = getPosition(i - j, i); mRight = mLeft + child.getMeasuredWidth(); if (mX >= mWidth) { mX = childw; mY += childh; j = i; mLeft = 0; mRight = mLeft + child.getMeasuredWidth(); mTop = mY + params.topMargin; } mBottom = mTop + child.getMeasuredHeight()+params.bottomMargin; mY = mTop; Position. left = mLeft; // The height must be recorded or the controls will be stacked together. position.top = mTop + 3; position.right = mRight; position.bottom = mBottom; map.put(child, position); } setMeasuredDimension(mWidth, mBottom); } @Override protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(0, 0); // default of 1px spacing } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); Position pos = (Position) map.get(child); if (pos ! = null) { child.layout(pos.left, pos.top, pos.right, pos.bottom); } else { Log.i("MyLayout", "error"); } } } private static class Position { int left, top, right, bottom; } public int getPosition(int IndexInRow, int childIndex) { if (IndexInRow > 0) { return getPosition(IndexInRow - 1, childIndex - 1) + getChildAt(childIndex - 1).getMeasuredWidth()+30; } return getPaddingLeft(); }}Copy the code

Use in layout as normal layout

 <com.jk.house.shopping_main.view.AutoNextLineLinearLayout
                android:id="@+id/tagLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="30dp"
                android:orientation="horizontal"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tv_tips" />
Copy the code

Apply to entity classes

// Add data tagLayout.removeAllViews() to AutoNextLineLinearLayout; for (int i = 0; i < detailsSymptomList.size(); i++) { DetailsSymptomBean detailsSymptomBean = detailsSymptomList.get(i); if (detailsSymptomBean.getDepartname().equals(type)) { for (String taboo : detailsSymptomBean.getTaboo()) { TextView textView = new TextView(getActivity()); textView.setText(taboo); textView.setBackgroundResource(R.drawable.selector_checkbox_bg_add_personal_info); AutoNextLineLinearLayout.LayoutParams params = new AutoNextLineLinearLayout .LayoutParams(AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT, AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT); Params. SetMargins (20, 20, 0, 0); textView.setLayoutParams(params); Textview.setpadding (40, 20, 40, 20); textView.setTextColor(getActivity().getResources(). getColorStateList(R.drawable.selector_checkbox_text_color_add_personal_info)); textView.setTextSize(30); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (view.isSelected()) { view.setSelected(false); tabooList.remove(((TextView) view).getText().toString()); } else { view.setSelected(true); tabooList.add(((TextView) view).getText().toString()); }}}); tagLayout.addView(textView); }}}Copy the code

conclusion

I tried to change the spacing and padding to control the display of labels

// This controls the location of the single tabs on the layout params.setMargins(20, 20, 0, 0); Textview.setpadding (40, 20, 40, 20);Copy the code

Each person’s project requirements may be different so we need to be flexible in how we arrange our content to achieve a satisfactory result

Finally, 2021, please work harder and continue to come on