Android流布局FlowLayout在搜索历史中的应用0920Word文档下载推荐.docx
《Android流布局FlowLayout在搜索历史中的应用0920Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Android流布局FlowLayout在搜索历史中的应用0920Word文档下载推荐.docx(31页珍藏版)》请在冰点文库上搜索。
db.execSQL(
"
createtablekw(_idintegerprimarykeyautoincrement,keywordvarchar,inputnointeger)"
);
//插入初始数据
db.execSQL("
insertintokw(keyword,inputno)values('
book'
'
0'
)"
书本'
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;
publicStringtoString(){
return"
Kw{"
+
id="
+id+
keyword='
"
+keyword+'
\'
'
inputno="
+inputno+
'
}'
;
KwDao.java
*封装对关键词表的数据库访问操作
publicclassKwDao{
publicKwDao(){}
publicKwDao(Contextcontext){
this.context=context;
privateContextcontext;
privateStringtablename="
kw"
*判断数据库中是否存在key
*@paramkey
*@return
publicbooleancontains(Stringkey){
List<
Kw>
list=newArrayList<
();
//实例化辅助类
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;
*插入一个全新的关键词
publiclonginsert(Stringkey){
//准备插入的数据
ContentValuesvalues=newContentValues();
//
values.put("
keyword"
key);
inputno"
1);
longid=db.insert(tablename,null,values);
Log.i("
spl"
"
insertid="
+id);
//关闭连接
*增加某个关键词的使用频度
publicvoidupdate(Stringkey){
updatekwsetinputno=inputno+1wherekeyword='
*根据"
用户的输入"
返回和这个词相关的关键词集合
publicList<
select(Stringkey){
select*from"
wherekeywordlike'
%"
%'
orderbyinputnodescLIMIT10"
Kwkw=newKw();
kw.setId(cursor.getInt(0));
kw.setKeyword(cursor.getString
(1));
kw.setInputno(cursor.getInt
(2));
list.add(kw);
//添加到集合
returnlist;
MyAdapter.java
流布局,核心类
*Createdbytaofangxinon16/4/16.
*自定义布局:
组件自动换行
publicclassFlowLayoutextendsViewGroup{
privateContextmContext;
privateintusefulWidth;
//thespaceofalinewecanuse(line'
swidthminusthesumofleftandrightpadding
//行间距
privateintlineSpacing=0;
//thespacingbetweenlinesinflowlayout
View>
childList=newArrayList();
Integer>
lineNumList=newArrayList();
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();
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<
this.getChildCount();
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
);
protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
intlineX=mPaddingLeft;
intlineWidth=r-l;
usefulWidth=lineWidth-mPaddingLeft-mPaddingRight;
intlineNum=0;
lineNumList.clear();
intleft=0;
inttop=0;
intright=0;
intbottom=0;
left=lineX+mlp.leftMargin;
top=lineY+mlp.topMargin;
right=lineX+mlp.leftMargin+childWidth;
bottom=lineY+mlp.topMargin+childHeight;
left=lineX;
top=lineY;
right=lineX+childWidth;
bottom=lineY+childHeight;
lineWidth){
lineNumList.add(lineNum);
lineX=mPaddingLeft;
lineNum=0;
child.layout(left,top,right,bottom);
lineNum++;
lineX+=spaceWidth;
//addthenumoflastline
*resortchildelementstouselinesasfewaspossible
publicvoidrelayoutToCompress(){
intchildCount=this.getChildCount();
if(0==childCount){
//noneedtosortifflowlayouthasnochildview
return;
intcount=0;
childCount;
Viewv=getChildAt(i);
if(vinstanceofBlankView){
//BlankViewisjusttomakechildslookinalignment,weshouldignorethemwhenwerelayout
count++;
View[]childs=newView[count];
int[]spaces=newint[count];
intn=0;
childs[n]=v;
LayoutParamschildLp=v.getLayoutParams();
intchildWidth=v.getMeasuredWidth();
MarginLayoutParamsmlp=(MarginLayoutParams)childLp;
spaces[n]=mlp.leftMargin+childWidth+mlp.rightMargin;
spaces[n]=childWidth;
n++;
int[]compressSpaces=newint[count];
count;
compressSpaces[i]=spaces[i]>
usefulWidth?
usefulWidth:
spaces[i];
sortToCompress(childs,compressSpaces);
this.rem