Android流布局FlowLayout在搜索历史中的应用0920.docx
- 文档编号:1712956
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:31
- 大小:80.09KB
Android流布局FlowLayout在搜索历史中的应用0920.docx
《Android流布局FlowLayout在搜索历史中的应用0920.docx》由会员分享,可在线阅读,更多相关《Android流布局FlowLayout在搜索历史中的应用0920.docx(31页珍藏版)》请在冰点文库上搜索。
Android流布局FlowLayout在搜索历史中的应用0920
Android-搜索历史的实现+FlowLayout应用-(2016-9-20)
项目:
SearchModel29环境:
AndroidStudio1.0
项目下载地址:
项目截图
项目结构
源码
Java
DBHelper.java
主要负责搜索关键词的存储和查询
/**
*SQLite辅助类
*/
publicclassDBHelperextendsSQLiteOpenHelper{
/**
*构造器
*@paramcontext
*/
publicDBHelper(Contextcontext){
super(context,"dbkeyword",null,1);
}
@Override
publicvoidonCreate(SQLiteDatabasedb){
//创建:
建表+插入数
db.execSQL(
"createtablekw(_idintegerprimarykeyautoincrement,keywordvarchar,inputnointeger)");
//插入初始数据
db.execSQL("insertintokw(keyword,inputno)values('book','0')");
db.execSQL("insertintokw(keyword,inputno)values('书本','0')");
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
//升级
}
}
Kw.java
主要负责搜索关键词的存储和查询
/**
*Createdby钧on2016/9/18.
*关键词实体类
*/
publicclassKw{
publicintid;
publicStringkeyword;
publicintinputno;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetKeyword(){
returnkeyword;
}
publicvoidsetKeyword(Stringkeyword){
this.keyword=keyword;
}
publicintgetInputno(){
returninputno;
}
publicvoidsetInputno(intinputno){
this.inputno=inputno;
}
@Override
publicStringtoString(){
return"Kw{"+
"id="+id+
",keyword='"+keyword+'\''+
",inputno="+inputno+
'}';
}
}
KwDao.java
主要负责搜索关键词的存储和查询
/**
*Createdby钧on2016/9/18.
*封装对关键词表的数据库访问操作
*/
publicclassKwDao{
publicKwDao(){}
publicKwDao(Contextcontext){
this.context=context;
}
privateContextcontext;
privateStringtablename="kw";
/**
*判断数据库中是否存在key
*@paramkey
*@return
*/
publicbooleancontains(Stringkey){
List
//实例化辅助类
DBHelperhelper=newDBHelper(context);
//获取SQLite数据库对象
SQLiteDatabasedb=helper.getReadableDatabase();
//执行查询命令,接收游标返回
Cursorcursor=db.rawQuery("selectcount(*)from"+this.tablename
+"wherekeyword='"+key+"'",null);
intnumber=0;
while(cursor.moveToNext()){//moveToNext:
移动到下一行
number=cursor.getInt(0);//count(*)
}
cursor.close();//游标关闭
db.close();//db关闭
if(number==0){
returnfalse;
}else{
returntrue;
}
}
/**
*插入一个全新的关键词
*@paramkey
*@return
*/
publiclonginsert(Stringkey){
DBHelperhelper=newDBHelper(context);
SQLiteDatabasedb=helper.getReadableDatabase();
//准备插入的数据
ContentValuesvalues=newContentValues();//
values.put("keyword",key);
values.put("inputno",1);
longid=db.insert(tablename,null,values);//
Log.i("spl","insertid="+id);
db.close();//关闭连接
returnid;
}
/**
*增加某个关键词的使用频度
*@paramkey
*/
publicvoidupdate(Stringkey){
DBHelperhelper=newDBHelper(context);
SQLiteDatabasedb=helper.getReadableDatabase();
db.execSQL("updatekwsetinputno=inputno+1wherekeyword='"+key+"'");
db.close();//关闭连接
}
/**
*根据"用户的输入"返回和这个词相关的关键词集合
*@paramkey
*@return
*/
publicList
List
//实例化辅助类
DBHelperhelper=newDBHelper(context);
//获取SQLite数据库对象
SQLiteDatabasedb=helper.getReadableDatabase();
//执行查询命令,接收游标返回
Cursorcursor=db.rawQuery("select*from"+this.tablename
+"wherekeywordlike'%"+key+"%'orderbyinputnodescLIMIT10",null);
while(cursor.moveToNext()){//moveToNext:
移动到下一行
Kwkw=newKw();
kw.setId(cursor.getInt(0));
kw.setKeyword(cursor.getString
(1));
kw.setInputno(cursor.getInt
(2));
list.add(kw);//添加到集合
}
cursor.close();//游标关闭
db.close();//db关闭
returnlist;
}
}
MyAdapter.java
流布局,核心类
/**
*Createdbytaofangxinon16/4/16.
*自定义布局:
组件自动换行
*/
publicclassFlowLayoutextendsViewGroup{
privateContextmContext;
privateintusefulWidth;//thespaceofalinewecanuse(line'swidthminusthesumofleftandrightpadding
//行间距
privateintlineSpacing=0;//thespacingbetweenlinesinflowlayout
List
List
publicFlowLayout(Contextcontext){
this(context,null);
}
publicFlowLayout(Contextcontext,AttributeSetattrs){
this(context,attrs,0);
}
publicFlowLayout(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
mContext=context;
TypedArraymTypedArray=context.obtainStyledAttributes(attrs,
R.styleable.FlowLayout);
//给行间距赋值
//lineSpacing=mTypedArray.getDimensionPixelSize(
//R.styleable.FlowLayout_lineSpacing,0);
lineSpacing=30;//px这里被我修改成固定的值了
mTypedArray.recycle();
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
intmPaddingLeft=getPaddingLeft();
intmPaddingRight=getPaddingRight();
intmPaddingTop=getPaddingTop();
intmPaddingBottom=getPaddingBottom();
intwidthSize=MeasureSpec.getSize(widthMeasureSpec);
intheightMode=MeasureSpec.getMode(heightMeasureSpec);
intheightSize=MeasureSpec.getSize(heightMeasureSpec);
intlineUsed=mPaddingLeft+mPaddingRight;
intlineY=mPaddingTop;
intlineHeight=0;
for(inti=0;i Viewchild=this.getChildAt(i); if(child.getVisibility()==GONE){ continue; } intspaceWidth=0; intspaceHeight=0; LayoutParamschildLp=child.getLayoutParams(); if(childLpinstanceofMarginLayoutParams){ measureChildWithMargins(child,widthMeasureSpec,0,heightMeasureSpec,lineY); MarginLayoutParamsmlp=(MarginLayoutParams)childLp; spaceWidth=mlp.leftMargin+mlp.rightMargin; spaceHeight=mlp.topMargin+mlp.bottomMargin; }else{ measureChild(child,widthMeasureSpec,heightMeasureSpec); } intchildWidth=child.getMeasuredWidth(); intchildHeight=child.getMeasuredHeight(); spaceWidth+=childWidth; spaceHeight+=childHeight; if(lineUsed+spaceWidth>widthSize){ //approachthelimitofwidthandmovetonextline lineY+=lineHeight+lineSpacing; lineUsed=mPaddingLeft+mPaddingRight; lineHeight=0; } if(spaceHeight>lineHeight){ lineHeight=spaceHeight; } lineUsed+=spaceWidth; } setMeasuredDimension( widthSize, heightMode==MeasureSpec.EXACTLY? heightSize: lineY+lineHeight+mPaddingBottom ); } @Override protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){ intmPaddingLeft=getPaddingLeft(); intmPaddingRight=getPaddingRight(); intmPaddingTop=getPaddingTop(); intlineX=mPaddingLeft; intlineY=mPaddingTop; intlineWidth=r-l; usefulWidth=lineWidth-mPaddingLeft-mPaddingRight; intlineUsed=mPaddingLeft+mPaddingRight; intlineHeight=0; intlineNum=0; lineNumList.clear(); for(inti=0;i Viewchild=this.getChildAt(i); if(child.getVisibility()==GONE){ continue; } intspaceWidth=0; intspaceHeight=0; intleft=0; inttop=0; intright=0; intbottom=0; intchildWidth=child.getMeasuredWidth(); intchildHeight=child.getMeasuredHeight(); LayoutParamschildLp=child.getLayoutParams(); if(childLpinstanceofMarginLayoutParams){ MarginLayoutParamsmlp=(MarginLayoutParams)childLp; spaceWidth=mlp.leftMargin+mlp.rightMargin; spaceHeight=mlp.topMargin+mlp.bottomMargin; left=lineX+mlp.leftMargin; top=lineY+mlp.topMargin; right=lineX+mlp.leftMargin+childWidth; bottom=lineY+mlp.topMargin+childHeight; }else{ left=lineX; top=lineY; right=lineX+childWidth; bottom=lineY+childHeight; } spaceWidth+=childWidth; spaceHeight+=childHeight; if(lineUsed+spaceWidth>lineWidth){ //approachthelimitofwidthandmovetonextline lineNumList.add(lineNum); lineY+=lineHeight+lineSpacing; lineUsed=mPaddingLeft+mPaddingRight; lineX=mPaddingLeft; lineHeight=0; lineNum=0; if(childLpinstanceofMarginLayoutParams){ MarginLayoutParamsmlp=(MarginLayoutParams)childLp; left=lineX+mlp.leftMargin; top=lineY+mlp.topMargin; right=lineX+mlp.leftMargin+childWidth; bottom=lineY+mlp.topMargin+childHeight; }else{ left=lineX; top=lineY; right=lineX+childWidth; bottom=lineY+childHeight; } } child.layout(left,top,right,bottom); lineNum++; if(spaceHeight>lineHeight){ lineHeight=spaceHeight; } lineUsed+=spaceWidth; lineX+=spaceWidth; } //addthenumoflastline lineNumList.add(lineNum); } /** *resortchildelementstouselinesasfewaspossible */ publicvoidrelayoutToCompress(){ intchildCount=this.getChildCount(); if(0==childCount){ //noneedtosortifflowlayouthasnochildview return; } intcount=0; for(inti=0;i Viewv=getChildAt(i); if(vinstanceofBlankView){ //BlankViewisjusttomakechildslookinalignment,weshouldignorethemwhenwerelayout continue; } count++; } View[]childs=newView[count]; int[]spaces=newint[count]; intn=0; for(inti=0;i Viewv=getChildAt(i); if(vinstanceofBlankView){ //BlankViewisjusttomakechildslookinalignment,weshouldignorethemwhenwerelayout continue; } childs[n]=v; LayoutParamschildLp=v.getLayoutParams(); intchildWidth=v.getMeasuredWidth(); if(childLpinstanceofMarginLayoutParams){ MarginLayoutParamsmlp=(MarginLayoutParams)childLp; spaces[n]=mlp.leftMargin+childWidth+mlp.rightMargin; }else{ spaces[n]=childWidth; } n++; } int[]compressSpaces=newint[count]; for(inti=0;i compressSpaces[i]=spaces[i]>usefulWidth? usefulWidth: spaces[i]; } sortToCompress(childs,compressSpaces); this.rem
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 流布 FlowLayout 搜索 历史 中的 应用 0920