流式布局

activity_main.xml

<?xml version="1.0" encoding="UTF-8"?>

-<LinearLayout tools:context="com.bwei.jiangbikaun.day0414demo.MainActivity" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">


-<LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent">

<EditText android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="8" android:id="@+id/et_info"/>

<Button android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="2" android:id="@+id/btn_sel" android:text="搜索"/>

</LinearLayout>

<com.bwei.jiangbikaun.day0414demo.view.FlawLayout android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" android:id="@+id/flawLayout"/>

<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/btn_delAll" android:text="清空历史记录"/>

</LinearLayout>

MainActivity.java

package com.bwei.jiangbikaun.day0414demo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.bwei.jiangbikaun.day0414demo.sqlite.HistoryDao;
import com.bwei.jiangbikaun.day0414demo.view.FlawLayout;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button btn_delAll;
    private EditText et_info;
    private FlawLayout flawLayout;
    private HistoryDao dao;
    private Button btn_sel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_delAll = findViewById(R.id.btn_delAll);
        dao = new HistoryDao(this);
        et_info = findViewById(R.id.et_info);
        btn_sel = findViewById(R.id.btn_sel);
        btn_sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = et_info.getText().toString();
                dao.insert(str);

            }
        });
        initChildViews();
        btn_delAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dao.delete();
            }
        });
    }

    private void initChildViews() {
        flawLayout = findViewById(R.id.flawLayout);
        List<String> select = dao.select();
        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 < select.size(); i ++){
            TextView view = new TextView(this);
            view.setText(select.get(i));
            view.setTextColor(Color.WHITE);
            view.setBackgroundColor(Color.GRAY);
            // view.setWidth(120);
            view.setPadding(15,3,15,4);
            //view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
            flawLayout.addView(view,lp);
        }
    }
}

FlawLayout.java

package com.bwei.jiangbikaun.day0414demo.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/14.
 */

public class FlawLayout extends ViewGroup {
    //存储所有子View
    private List<List<View>> mAllChildViews = new ArrayList<>();
    //每一行的高度
    private List<Integer> mLineHeight = new ArrayList<>();
    public FlawLayout(Context context) {
        this(context,null);
    }

    public FlawLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlawLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //父控件传进来的宽度和高度以及对应的测量模式
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        //如果当前ViewGroup的宽高为wrap_content的情况
        int width = 0;//自己测量的宽度
        int height = 0;//自己测量的高度
        //记录每一行的宽度和高度
        int lineWidth = 0;
        int lineHeight = 0;

        //获取子view的个数
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i ++){
            View child = getChildAt(i);
            //测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            //子View占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //子View占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //换行时候
            if(lineWidth + childWidth > sizeWidth){
                //对比得到最大的宽度
                width = Math.max(width, lineWidth);
                //重置lineWidth
                lineWidth = childWidth;
                //记录行高
                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);
    }

    @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){
                //记录LineHeight
                mLineHeight.add(lineHeight);
                //记录当前行的Views
                mAllChildViews.add(lineViews);
                //重置行的宽高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                //重置view的集合
                lineViews = new ArrayList();
            }lineViews.add(child);
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);

        }
        //处理最后一行
        mLineHeight.add(lineHeight);
        mAllChildViews.add(lineViews);

        //设置子View的位置
        int left = 0;
        int top = 0;
        //获取行数
        int lineCount = mAllChildViews.size();
        for(int i = 0; i < lineCount; i ++){
            //当前行的views和高度
            lineViews = mAllChildViews.get(i);
            lineHeight = mLineHeight.get(i);
            for(int j = 0; j < lineViews.size(); j ++){
                View child = lineViews.get(j);
                //判断是否显示
                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;
        }

    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}

MySqlHelper.java

package com.bwei.jiangbikaun.day0414demo.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2018/4/14.
 */

public class MySqlHelper extends SQLiteOpenHelper{
    public MySqlHelper(Context context) {
        super(context, "select.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table selHistory(id integer primary key autoincrement,history text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

HistoryDao.java

package com.bwei.jiangbikaun.day0414demo.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/14.
 */

public class HistoryDao {
    private MySqlHelper mySqlHelper;

    public HistoryDao(Context context) {
        mySqlHelper = new MySqlHelper(context);
    }

    //添加
    public void insert(String history){
        SQLiteDatabase sqLiteDatabase = mySqlHelper.getWritableDatabase();


        ContentValues value = new ContentValues();

        value.put("history",history);

        sqLiteDatabase.insert("selHistory",null,value);
        Log.e("jiang","添加了数据");
    }
    //查询
    public List<String> select(){
        List<String> data = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = mySqlHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("selHistory", null, null, null, null, null, null);
        while(cursor.moveToNext()){
            String history = cursor.getString(cursor.getColumnIndex("history"));
            data.add(history);
        }
        return data;
    }

    //删除
    public void delete(){
        SQLiteDatabase sqLiteDatabase = mySqlHelper.getWritableDatabase();
        int selHistory = sqLiteDatabase.delete("selHistory", null, null);
        Log.e("jiang","删除条目"+selHistory);
    }
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页