android ----- custom View to achieve search added to the history

===============Flowlayout=============

public class XCflowLayout extends ViewGroup {
    //储存所有子View
    List<List<View>> mAllChildV5iews = new ArrayList<>();
    //每行的高度
    List<Integer> mLineHeight=new ArrayList<>();


    public XCflowLayout(Context context) {
        this(context,null);
    }


    public XCflowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }


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


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        //测量宽度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        //measure height
        int sizeheight = MeasureSpec.getSize(heightMeasureSpec);
        int modeheight = MeasureSpec.getMode(heightMeasureSpec);
        //if the width and height are In the case of wrap_content
        int width=0;
        int height=0;
        //Record the width and height of each line
        int lineWidth=0;
        int lineHeight=0;
        //Get the number of a view
        int childCount = getChildCount();
        //Get it in a loop The value of each view
        for(int i=0;i<childCount;i++){
            View child = getChildAt(i);
            //Measure the width and height of the child View
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
            //Get LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            //The width occupied by the child View
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //child The height occupied by the View
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //When wrapping lines
            if(lineWidth+childWidth>sizeWidth){
                //Compare to get the maximum width
                width=Math.max(width, lineWidth);
                //Reset lineWidth
                lineWidth=childWidth;
                //Record line
                height height+= lineHeight;
                lineHeight=childHeight;
            }else{
                //叠加行宽
                lineWidth+=childWidth;
                //得到最大行高
                lineHeight=Math.max(lineHeight,childHeight);
            }
            //处理最后一个子View
            if(i==childCount-1){
                width=Math.max(width,lineWidth);
                height+=lineHeight;
            }
        }
        //wrap_content
        setMeasuredDimension(modeWidth==MeasureSpec.EXACTLY ? sizeWidth:width,modeheight==MeasureSpec.EXACTLY ? sizeheight:height);


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);




    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mAllChildViews.clear();
        mLineHeight.clear();


        //获得当前ViewGroup的宽度
        int width = getWidth();


        int lineWidth=0;
        int lineHeight=0;
        // 记录当前行的View
        List<View> lineViews=new ArrayList<View>();
        int childCount = getChildCount();
        for(int i=0;i<childCount;i++){
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            //如果需要换行
            if(childWidth+lineWidth+lp.leftMargin+lp.rightMargin>width){
                //record LineHeight
                mLineHeight.add(lineHeight);
                //record current Views
                mAllChildViews.add(lineViews);
                //reset line height
                lineWidth=0 ;
                lineHeight=childHeight+lp.topMargin+lp.bottomMargin;
                //Reset view collection
                lineViews=new ArrayList();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin; lineHeight
            = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
            lineViews.add(child);
        }
        //Process the last line
        mLineHeight.add(lineHeight);
        mAllChildViews.add(lineViews);


        //Set the position of the child View
        int left = 0;
        int top = 0;
        //Get the number of lines
        int lineCount = mAllChildViews.size();
        for(int i = 0;i<lineCount;i++){
            //Views and height of the current line
            lineViews=mAllChildViews.get(i);
            lineHeight=mLineHeight.get(i);
            for (int j=0;j<lineViews.size(); j++){
                View child=lineViews.get(j);
                //Determine whether to display
                if(child.getVisibility()==View.GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                //进行子View进行布局
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 0;
            top += lineHeight;
        }
    }
    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub


        return new MarginLayoutParams(getContext(), attrs);
    }

}




====================== MainActivity =====================

public class MainActivity extends AppCompatActivity {
    private String mNames[] = {
            "Study hard", "Go up every day", "I'm going crazy", "I
            can't learn, what should I do", "It feels like a book from heaven", "God , please help me!!!!!",
            "Eight-dimensional don't fall, the last shift is old", "Android is a pit", "Jumping down is the endless darkness",
            "Old drivers drive, please fasten your condoms" , "Your fake bags of more than 10,000 are so beautiful", "Tencent Video",
            "Wrong way", "God rewards hard work, the constant can win", "Douyin Album", "I don't want to do it anymore, I want to rest! !!!!!"
    };
    private XCflowLayout mFlowLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initChildViews();


    }
    private void initChildViews(){
        mFlowLayout = (XCflowLayout) findViewById(R.id.flowlayout);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 5;
        lp.rightMargin = 5;
        lp.topMargin = 5;
        lp.bottomMargin = 5;
        for(int i = 0; i < mNames.length; i ++){
            TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.BLACK);
            view.setBackgroundColor(Color.GRAY);
            mFlowLayout.addView(view,lp);
        }
    }

}


===============xml=================


<com.aaaa.why.myapplication.MyGroup
android:id="@+id/mygroup"
android:layout_width="wrap_content"
android:layout_height="300dp">
</com.aaaa.why.myapplication.MyGroup>
<Button
android:id="@+id/button"
android:textSize="25dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空历史记录"
/>            

                            



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324474063&siteId=291194637