流式布局

组合控件 headerview视图
public class MyHeaderView extends LinearLayout {
private EditText searchText;
private TextView addText;

public MyHeaderView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //加载布局
    LayoutInflater.from(context).inflate(R.layout.head_view, this);
    searchText = findViewById(R.id.search_text);
    addText = findViewById(R.id.add_text);
}

//搜索
public String getSearchText(){
    return searchText.getText().toString().trim();
}
//添加
public TextView getAddTexts(){
    return addText;
}

}

xml页面

<EditText
    android:id="@+id/search_text"
    android:layout_width="0dp"
    android:layout_weight="6"
    android:drawablePadding="5dp"
    android:drawableLeft="@mipmap/ic_launcher_round"
    android:layout_height="wrap_content"
    android:hint="搜索"/>
<TextView
    android:textColor="@color/colorPrimaryDark"
    android:id="@+id/add_text"
    android:layout_weight="1"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="添加"/>

**流式布局页面 MyFloatLayout **
public class MyFloatLayout extends LinearLayout{
private int mScreenWidth;
private String mColor;

public MyFloatLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    //获取屏幕宽度
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    mScreenWidth = metrics.widthPixels;
    setOrientation(VERTICAL);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupDemoView);
    if (typedArray != null) {
        mColor = (String) typedArray.getText(R.styleable.GroupDemoView_textColor);
        typedArray.recycle();//释放资源
    }
}

public void setData(ArrayList<String> data) {
    LinearLayout linearLayout = getLin();
    for (int i = 0; i < data.size(); i++) {
        final String temp = data.get(i);
        int numWidth = 0;
        //获得子控件数量
        int childCount = linearLayout.getChildCount();
        for (int j = 0; j < childCount; j++) {
            //获得每行linelayout的所有子控件TextView
            TextView tv = (TextView) linearLayout.getChildAt(j);
            //获取TextView的leftmargin并测量tv的宽高
            LayoutParams params = (LayoutParams) tv.getLayoutParams();
            int leftMargin = params.leftMargin;
            tv.measure(getMeasuredWidth(), getMeasuredHeight());
            numWidth += tv.getMeasuredWidth() + leftMargin + tv.getPaddingRight() + tv.getPaddingLeft();
        }
        //得到TextView
        TextView textView = getText();
        //设置属性
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.leftMargin = 15;
        params.topMargin = 10;
        textView.setLayoutParams(params);
        //设置值
        textView.setText(temp);
        //再次测量宽高
        textView.measure(textView.getMeasuredWidth(),textView.getMeasuredHeight());
        //得到文本的最终大小
        int dataTextWidth = textView.getMeasuredWidth()+textView.getPaddingLeft()+textView.getPaddingRight();
        //判断 一行所有子控件的宽度是否大于屏幕的宽度
        if(mScreenWidth >= numWidth + dataTextWidth){
            linearLayout.addView(textView);//小于的话,再次添加一个子控件TextView
        }else{
            //换行
            linearLayout = getLin();
            //重新添加view
            linearLayout.addView(textView);
        }

    }
}

//初始化子LinearLayout
public LinearLayout getLin() {
    //初始化
    LinearLayout linearLayout = new LinearLayout(getContext());
    //设置组件大小
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    linearLayout.setLayoutParams(params);
    this.addView(linearLayout);//重新添加view
    return linearLayout;
}

public void removeChildView(){
    removeAllViews();//移除所有子控件
}

//初始化TextView
public TextView getText() {
    TextView tv = new TextView(getContext());
    //要显示的TextView的颜色,大小
    tv.setTextSize(20);
    tv.setTextColor(Color.parseColor(mColor));//设置文字颜色
    tv.setPadding(10,3,10,3);
    return tv;
}

}

main页面
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private String[] data = {"流感", "咳嗽", "过敏", "发烧", "感冒", "湿疹", "便秘", "痔疮", "协和", "鼻炎", "失眠", "痛风", "上火", "脚气", "抑郁症", "性欲", "乳腺增生", "头晕", "腰痛"};
private MyHeaderView myheader_view;
private TextView delete;
private MyFloatLayout my_float_history;
private MyFloatLayout my_float;
//热门搜索
private ArrayList<String> mList = new ArrayList<>();
private ArrayList<String> mHistory = new ArrayList<>();
private MyDao dao;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dao = new MyDao(MainActivity.this);
    mHistory = dao.selectName();

    initData();
    initView();

    if(!mHistory.isEmpty()){
        my_float_history.setData(mHistory);
    }
}

private void initData() {
    for (int i = 0; i <data.length ; i++) {
        mList.add(data[i]);
    }
}

private void initView() {
    myheader_view = (MyHeaderView) findViewById(R.id.myheader_view);
    myheader_view.getAddTexts().setOnClickListener(this);
    delete = (TextView) findViewById(R.id.delete);
    delete.setOnClickListener(this);
    my_float_history = (MyFloatLayout) findViewById(R.id.my_float_history);
    my_float = (MyFloatLayout) findViewById(R.id.my_float);
    my_float.setData(mList);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        //添加
        case R.id.add_text:
            //获取输入框的值
            String name = myheader_view.getSearchText().trim();
            //判断name是否为空
            if(name.equals("")){
                Toast.makeText(MainActivity.this,"不能为空",Toast.LENGTH_LONG).show();
            }else {
                //添加到数据库
                dao.insert(myheader_view.getSearchText().trim());
                //清空
                my_float_history.removeAllViews();
                //添加到搜索历史集合中mHistory
                mHistory.add(name);
                //调用my_float_history中的方法展示数据
                my_float_history.setData(mHistory);
            }
            break;
        //删除
        case R.id.delete:
            //数据库删除
            dao.delete();
            //清空所有数据
            my_float_history.removeAllViews();
            mHistory.clear();//清空历史记录集合
            break;
    }
}

}

main.xml页面

<!--标题栏-->
<com.example.week1_1.weight.MyHeaderView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/myheader_view"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:textColor="@color/colorPrimaryDark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索历史" />
    <TextView
        android:textColor="@color/colorPrimaryDark"
        android:id="@+id/delete"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="删除"/>
</RelativeLayout>

<com.example.week1_1.weight.MyFloatLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/my_float_history"
    app:textColor="@color/colorPrimaryDark"/>

<TextView
    android:textColor="@color/colorPrimaryDark"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="热门搜索"/>

<com.example.week1_1.weight.MyFloatLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/my_float"
    app:textColor="@color/colorPrimaryDark"/>
</LinearLayout>

dao层
public class MyDao{
private MyHelper myHelper;
private SQLiteDatabase db;
private Context mContext;
public MyDao(Context context){
myHelper = new MyHelper(context);
db = myHelper.getWritableDatabase();
mContext = context;
}

//添加数据
public void insert(String name){
    ContentValues cv = new ContentValues();
    cv.put("name",name);
    db.insert("news",null,cv);
    Toast.makeText(mContext,"插入成功",Toast.LENGTH_LONG).show();
}

//查询
public ArrayList<String> selectName(){
    ArrayList<String> list = new ArrayList<>();
    Cursor cursor = db.query("news", null, null, null, null, null, null);
    while (cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndex("name"));
        list.add(name);
    }
    return list;
}

public void delete(){
    db.execSQL("delete from news");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值