//使用GreenDao数据库需要配置
//budile配置
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
//model需要配置
apply plugin: 'org.greenrobot.greendao' greendao { schemaVersion 1 daoPackage 'com.greendao.gen' targetGenDir 'src/main/java' }
compile 'org.greenrobot:greendao:3.2.0'
//首先自定义流失布局
package com.example.jingdong.Liushi; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /** * Created by huoxuebin on 2018/4/26. */ public class XCFlowLayout extends ViewGroup { //存储所有子View private List<List<View>> mAllChildViews = new ArrayList<>(); //每一行的高度 private List<Integer> mLineHeight = new ArrayList<>(); public XCFlowLayout(Context context) { this(context, null); // TODO Auto-generated constructor stub } public XCFlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); // TODO Auto-generated constructor stub } public XCFlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub //父控件传进来的宽度和高度以及对应的测量模式 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); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub 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(); } lineWidth += childWidth + lp.leftMargin + lp.rightMargin; lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin); lineViews.add(child); } //处理最后一行 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; } } /** * 与当前ViewGroup对应的LayoutParams */ @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { // TODO Auto-generated method stub return new MarginLayoutParams(getContext(), attrs); }
//然后就是搜索页的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/line" android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal" > <LinearLayout android:id="@+id/linearlayout" android:background="@drawable/serach_kuang" android:layout_weight="8" android:layout_width="0dp" android:layout_gravity="center_vertical" android:layout_height="50dp"> <!-- android:imeOptions="actionSearch" --> <EditText android:imeOptions="actionSearch" android:singleLine="true" android:background="@null" android:id="@+id/edname" android:layout_width="0dp" android:layout_height="50dp" android:hint="性感女士内衣全场1折" android:layout_weight="1" android:gravity="center_vertical" /> </LinearLayout> <LinearLayout android:gravity="center_horizontal" android:orientation="vertical" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content"> <TextView android:background="@drawable/home_title_bar_search_corner_bg" android:id="@+id/sosuo" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="20dp" android:gravity="center" android:text="搜索"/> </LinearLayout> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="热搜" android:textColor="#000000" android:textSize="20dp" /> <com.example.month1.liushi.Liushi android:id="@+id/flowlayout" android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="80dp" > </com.example.month1.liushi.Liushi> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/yin" > <TextView android:id="@+id/jilu" android:layout_below="@id/line" android:layout_width="match_parent" android:layout_height="30dp" android:text="历史记录" android:textColor="#000000" android:textSize="22dp" /> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv" android:layout_below="@id/jilu"> </ListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:background="@drawable/home_title_bar_search_corner_bg" android:onClick="delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="全部清除" android:layout_alignParentBottom="true" tools:ignore="OnClick" /> </RelativeLayout> </LinearLayout> </LinearLayout>
//流式布局代码以及GreenDao代码
package com.example.jingdong.view; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.example.jingdong.Liushi.XCFlowLayout; import com.example.jingdong.R; import com.example.jingdong.bean.User; import com.greendao.gen.DaoMaster; import com.greendao.gen.DaoSession; import com.greendao.gen.UserDao; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class SerachActivity extends AppCompatActivity { //定义数组 private String mNames[] = { "男装","电脑","手机","apple", "坚果","苹果", "戴尔","阿迪短袖","迪凯斯男装", "男士皮鞋" }; private TextView textView; private UserDao userDao; private XCFlowLayout mFlowLayout; private List<String> list = new ArrayList<>(); private String sname; private User user; private Adapter adapter; private EditText editText; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_serach); ButterKnife.bind(this); textView = findViewById(R.id.sosuo); editText = findViewById(R.id.edname); lv = findViewById(R.id.lv); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sname = editText.getText().toString(); //跳转展示页面 if("".equals(sname)){ Toast.makeText(SerachActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show(); } else{ //将数据添加到数据库 user = new User(null, sname); long insert = userDao.insert(user); list.add(sname); adapter = new Adapter(SerachActivity.this, list); lv.setAdapter(adapter); Intent intent = new Intent(SerachActivity.this,ZhanshiActivity.class); intent.putExtra("name", sname); startActivity(intent); } } }); editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { String s = editText.getText().toString(); Intent intent = new Intent(SerachActivity.this,ZhanshiActivity.class); intent.putExtra("name",s); startActivity(intent); return true; } return false; } }); initChildViews(); //调取Greendao DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"user_db"); SQLiteDatabase database = helper.getReadableDatabase(); DaoMaster daoMaster = new DaoMaster(database); DaoSession session = daoMaster.newSession(); userDao = session.getUserDao(); //长按删除 lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { userDao.deleteByKey((long) i); list.remove(i); // adapter.notifyDataSetChanged(); lv.setAdapter(new Adapter(SerachActivity.this,list)); return false; } }); //点击传值到搜索框 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { editText.setText(list.get(i).toString()); } }); //查询数据库 List<User> users = userDao.loadAll(); if (users!=null){ for (int i=0;i<users.size();i++){ String sname = users.get(i).getName(); list.add(sname); } lv.setAdapter(new Adapter(SerachActivity.this,list)); } } //搜索,添加数据库 //全部删除 public void delete(View view) { userDao.deleteAll(); list.clear(); Adapter adapter = new Adapter(SerachActivity.this, list); lv.setAdapter(adapter); } //适配器 class Adapter extends BaseAdapter { Context context; List<String> list; public Adapter(Context context, List<String> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { TextView textView = new TextView(context); textView.setText(list.get(i)); textView.setTextSize(18); return textView; } } //流式布局 private void initChildViews() { mFlowLayout = 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 ++){ final TextView view1 = new TextView(this); view1.setText(mNames[i]); view1.setTextColor(Color.BLACK); view1.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg)); mFlowLayout.addView(view1,lp); view1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String lname = view1.getText().toString(); //Toast.makeText(SerachActivity.this,view1.getText().toString() , Toast.LENGTH_SHORT).show(); Intent intent = new Intent(SerachActivity.this,ZhanshiActivity.class); intent.putExtra("name",lname); startActivity(intent); } }); } } }
//drawable文件
//给流失布局设置边框
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#d7ebebeb" /> <corners android:radius="10dp" /> <padding android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp" /> </shape>
//Greendao Dao类
package com.example.month1; import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Generated; @Entity public class User { @Id(autoincrement = true) private Long id; private String name; @Generated(hash = 873297011) public User(Long id, String name) { this.id = id; this.name = name; } @Generated(hash = 586692638) public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }