public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "demo.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table history(_id INTEGER primary key autoincrement,content varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MySearchView mySearchView;
private MyFlowLayout myFlowLayout;
private MyListView myListView;
private TextView sousuo;
private MyOpenHelper myOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatabase();
}
private void initDatabase() {
myOpenHelper = new MyOpenHelper(this);
SQLiteDatabase database = myOpenHelper.getReadableDatabase();
}
private void initViews() {
mySearchView = (MySearchView) findViewById(R.id.mySearchView);
myFlowLayout = (MyFlowLayout) findViewById(R.id.flow_hot_search);
myListView = (MyListView) findViewById(R.id.lv_search);
sousuo = (TextView) findViewById(R.id.sousuo);
sousuo.setOnClickListener(this);
ImageView dele = (ImageView) findViewById(R.id.iv_delete);
dele.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sousuo:
String con = mySearchView.getContent();
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("content", con);
db.insert("history", null, contentValues);
db.close();
SQLiteDatabase db1 = myOpenHelper.getReadableDatabase();
Cursor cursor = db1.query("history", null, null, null, null, null, null);
List<String> list = new ArrayList<>();
while (cursor.moveToNext()) {
String content = cursor.getString(cursor.getColumnIndex("content"));
list.add(content);
}
db1.close();
MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
myListView.setAdapter(myAdapter);
break;
case R.id.iv_delete:
SQLiteDatabase db2 = myOpenHelper.getReadableDatabase();
db2.delete("history",null,null);
db2.close();
SQLiteDatabase db3 = myOpenHelper.getReadableDatabase();
Cursor cursor3 = db3.query("history", null, null, null, null, null, null);
List<String> list1 = new ArrayList<>();
while (cursor3.moveToNext()) {
String content = cursor3.getString(cursor3.getColumnIndex("content"));
list1.add(content);
}
db3.close();
MyAdapter myAdapter2 = new MyAdapter(MainActivity.this, list1);
myListView.setAdapter(myAdapter2);
break;
}
}
}
public class MyAdapter extends BaseAdapter {
private final Context context;
private final List<String> list;
public MyAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(list.get(position));
textView.setTextSize(20);
return textView;
}
}
public class MyFlowLayout extends ViewGroup{
private int useWidth;
private int MaxHeight;
private int HorizonytalSpace = 5;
private int VerticalSpaace = 5;
private Line mLine;
private int MaxLine = 100;
private ArrayList<Line> LineList = new ArrayList<Line>();
public MyFlowLayout(Context context) {
super(context);
}
public MyFlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,widthMode == MeasureSpec.EXACTLY?MeasureSpec.AT_MOST:widthMode);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,heightMode == MeasureSpec.EXACTLY?MeasureSpec.AT_MOST:heightMode);
childView.measure(childWidthMeasureSpec,childHeightMeasureSpec);
int childWidth = childView.getMeasuredWidth();
int childHeight = childView.getMeasuredHeight();
useWidth += childWidth;
if(mLine == null){
mLine = new Line();
}
if(useWidth < width){
mLine.addView(childView);
useWidth += HorizonytalSpace;
if(useWidth >= width){
if(!newLine()){
break;
}
}
}else {
if(mLine.getLineCount() == 0){
mLine.addView(childView);
LineList.add(mLine);
if(!newLine()){
break;
}
}else {
if(!newLine()){
break;
}
mLine.addView(childView);
useWidth += HorizonytalSpace + childWidth;
}
}
if (mLine != null && mLine.getLineCount() > 0
&& !LineList.contains(mLine)) {
LineList.add(mLine);
}
}
int Totalwidth = MeasureSpec.getSize(widthMeasureSpec);
int TotalHeight = 0;
for (int i = 0; i <LineList.size() ; i++) {
TotalHeight += LineList.get(i).MaxHeight;
}
TotalHeight += (LineList.size() - 1)*VerticalSpaace + getPaddingTop() + getPaddingBottom();
setMeasuredDimension(Totalwidth,TotalHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int Top = getPaddingTop();
int Left = getPaddingLeft();
for (int i = 0; i < LineList.size(); i++) {
Line line = LineList.get(i);
line.layoutView(Left,Top);
Top += line.MaxHeight + VerticalSpaace;
}
}
private boolean newLine(){
if(LineList.size() < MaxLine){
LineList.add(mLine);
mLine = new Line();
useWidth = 0;
MaxHeight = 0;
return true;
}
return false;
}
class Line{
private int mLineWidth = 0;
private int MaxHeight = 0;
private ArrayList<View> viewlist = new ArrayList<View>();
public void addView(View view){
viewlist.add(view);
mLineWidth += view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
MaxHeight = MaxHeight < childHeight?childHeight:MaxHeight;
}
public int getLineCount(){
return viewlist.size();
}
public void layoutView(int l,int t){
int Left = l;
int Top = t;
int childCount = viewlist.size();
int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -(childCount-1) * HorizonytalSpace;
int surplusWidth = width - mLineWidth;
if(surplusWidth > 0){
int widthOffSet = (int) (surplusWidth * 1.0f/viewlist.size() + 0.5f);
for (int i = 0; i < viewlist.size(); i++) {
View view = viewlist.get(i);
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
childWidth += widthOffSet;
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth,MeasureSpec.EXACTLY);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight,MeasureSpec.EXACTLY);
view.measure(childWidthMeasureSpec,childHeightMeasureSpec);
int TopOffSet = (MaxHeight - childHeight) / 2;
TopOffSet = TopOffSet>0?TopOffSet:0;
view.layout(Left,Top+ TopOffSet,Left +childWidth,Top + TopOffSet + childHeight);
Left += HorizonytalSpace + childWidth;
}
}else{
}
}
}
public void setHorizontalSpacing(int horizonytalSpace) {
HorizonytalSpace = horizonytalSpace;
}
public void setVerticalSpacing(int verticalSpaace) {
VerticalSpaace = verticalSpaace;
}
}
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMea = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMea);
}
}
public class MySearchView extends LinearLayout {
private EditText et_search;
public MySearchView(Context context) {
this(context, null);
}
public MySearchView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MySearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.searchview_layout, this);
et_search = view.findViewById(R.id.et_search);
}
public String getContent() {
return et_search.getText().toString();
}
}
seach_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="100dp"
android:height="30dp" />
<corners android:radius="15dp" />
<solid android:color="@color/colorTextGroupBg"/>
</shape>
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.rookie.lymth.test1.MySearchView
android:id="@+id/mySearchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></com.rookie.lymth.test1.MySearchView>
<TextView
android:id="@+id/sousuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20px"
android:text="热门搜索"
android:textSize="36px" />
<com.rookie.lymth.test1.MyFlowLayout
android:id="@+id/flow_hot_search"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F00"
android:text="电饭煲" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F00"
android:text="手机" />
</com.rookie.lymth.test1.MyFlowLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20px">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="历史搜索"
android:textSize="36px" />
<ImageView
android:id="@+id/iv_delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/cha" />
</LinearLayout>
<com.rookie.lymth.test1.MyListView
android:id="@+id/lv_search"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.rookie.lymth.test1.MyListView>
</LinearLayout>
</ScrollView>
searchview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/seach_bg"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="6px">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/a_4" />
<EditText
android:id="@+id/et_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_margin="6px"
android:layout_weight="1"
android:background="@null"
android:hint=" 请输入要搜索的关键词"
android:paddingBottom="10px"
android:paddingLeft="30px"
android:paddingTop="10px"
android:textColor="#333"
android:textColorHint="#666"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<color name="colorTextGroupBg">#55999999</color>