在 主build.gradle中
repositories { google() jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.0.0' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
在app的build.gradle中
在开始:
apply plugin: 'org.greenrobot.greendao' // apply plugin
在dependencies上面加
greendao { schemaVersion 1//数据库版本号 daoPackage 'com.com.sky.downloader.greendao'//设置DaoMaster、DaoSession、Dao包名 targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 //targetGenDirTest:设置生成单元测试目录 //generateTests:设置自动生成单元测试用例 }
最后到依赖:
implementation 'org.greenrobot:greendao:3.2.2' // add library
User类
@Entity(nameInDb = "t_user") public class User { @Id(autoincrement = true) private Long id; @Property 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; } @Override public String toString() { return name; } }
MyApp类 初始化
public class MyApp extends Application{ private static DaoSession daoSession; @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "text.db"); DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb()); daoSession = daoMaster.newSession(); } public static DaoSession getDaoSession(){ return daoSession; } }
MainActivity类
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String mNames[] = { "笔记本", "空气净化器", "安卓手机", "超级大号圆珠笔", "空气滤芯", "超级大号钢笔", "气垫cc", "防晒霜" }; /** * 请输入商品 */ private EditText mSuos; /** * 搜索 */ private Button mBtSuo; private MyViewGroup mMvg; private ListView mLv; /** * 清空历史记录 */ private Button mDel; private UserDao userDao; private String name; private List<User> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initChildViews(); DaoSession daoSession = MyApp.getDaoSession(); userDao = daoSession.getUserDao(); } private void initChildViews() { 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.setTextSize(15); view.setPadding(15, 3, 15, 4); mMvg.addView(view, lp); final int finalI = i; view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mSuos.setText(mNames[finalI]); list = userDao.queryBuilder().build().list(); User user2 = new User(); user2.setName(mNames[finalI]); for (int i = 0; i <list.size() ; i++) { String name1 = list.get(i).getName(); if(mNames[finalI].equals(name1)){ Toast.makeText(MainActivity.this,"数据库已有",Toast.LENGTH_SHORT).show(); return; } } userDao.insert(user2); select(); } }); } } private void initView() { mSuos = (EditText) findViewById(R.id.suos); mBtSuo = (Button) findViewById(R.id.bt_suo); mBtSuo.setOnClickListener(this); mMvg = (MyViewGroup) findViewById(R.id.mvg); mLv = (ListView) findViewById(R.id.lv); mDel = (Button) findViewById(R.id.del); mDel.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.bt_suo: name = mSuos.getText().toString().trim(); list = userDao.queryBuilder().build().list(); User user = new User(); user.setName(name); for (int i = 0; i <list.size() ; i++) { String name1 = list.get(i).getName(); if(name.equals(name1)){ Toast.makeText(MainActivity.this,"数据库已有",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this, ListActivity.class); intent.putExtra("name",name); startActivity(intent); return; } } userDao.insert(user); select(); Intent intent = new Intent(MainActivity.this, ListActivity.class); intent.putExtra("name",name); startActivity(intent); break; case R.id.del: userDao.deleteAll(); select(); break; } } private void select(){ list = userDao.queryBuilder().build().list(); ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1, list); mLv.setAdapter(arrayAdapter); } }
流式布局MyViewGroup类
public class MyViewGroup extends ViewGroup { //存储所有子View private List<List<View>> mAllChildViews = new ArrayList<>(); //每一行的高度 private List<Integer> mLineHeight = new ArrayList<>(); public MyViewGroup(Context context) { this(context, null); // TODO Auto-generated constructor stub } public MyViewGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); // TODO Auto-generated constructor stub } public MyViewGroup(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="com.example.administrator.yuekaomoni.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="45dp" android:layout_height="45dp" android:src="@drawable/leftjiantou"/> <EditText android:layout_width="350dp" android:layout_height="45dp" android:id="@+id/suos" android:hint="请输入商品"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" android:textColor="#fff" android:background="#FFEFA718" android:id="@+id/bt_suo"/> </LinearLayout> <com.example.administrator.yuekaomoni.sousuo.MyViewGroup android:layout_width="match_parent" android:layout_height="130dp" android:layout_marginTop="20dp" android:id="@+id/mvg"></com.example.administrator.yuekaomoni.sousuo.MyViewGroup> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv"></ListView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清空历史记录" android:id="@+id/del"/> </LinearLayout>