您的位置:首页 > 其它

实现商品的搜索功能

2017-12-16 08:49 441 查看
//activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.test.sousuo.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<" />

<EditText
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:hint="最新上市,内衣三免一,服装免费送秩序1分钱" />

<Button
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:onClick="add"
android:layout_gravity="center" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:text="热搜" />

<test.bwie.com.sousuo.liushi
android:id="@+id/ls"
android:layout_width="match_parent"
android:layout_height="80dp"></test.bwie.com.sousuo.liushi>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="历史记录"
android:textSize="24dp" />

<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/lv">
</ListView>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空历史记录"
android:layout_gravity="center"
android:gravity="center"
android:onClick="delall"
android:visibility="invisible"
android:id="@+id/btn"  />

</LinearLayout>


//MainActivity

public class MainActivity extends AppCompatActivity {
private liushi mFlowLayout;
private EditText name;
private Dao dao;
private ListView lv;
private ArrayAdapter<String> adapter;
private List<String> sel;
private Button btn;
List<String> a=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFlowLayout = (liushi) findViewById(R.id.ls);

name = (EditText) findViewById(R.id.name);
lv = (ListView) findViewById(R.id.lv);
btn = (Button) findViewById(R.id.btn);
dao = new Dao(MainActivity.this);
sel = dao.sel();
adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, sel);

lv.setAdapter(adapter);

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int d, long l) {
AlertDialog.Builder ab=new AlertDialog.Builder(MainActivity.this);
ab.setTitle("是否删除");
Log.d("aaa",sel.get(d).toString());
ab.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
int delyi = dao.delyi(sel.get(d).toString());

if (delyi==1){
zhanshi();
}
}
});
/*  */
ab.setNegativeButton("取消",null);

ab.show();
return false;
}
});

if (sel.size()>0){
btn.setVisibility(View.VISIBLE);
}else if(sel.size()==0)
{
btn.setVisibility(View.INVISIBLE);
}
initChildViews();
}

private void zhanshi() {
List<String> sel4 = dao.sel();
ArrayAdapter<String> ada = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, sel4);
lv.setAdapter(ada);
}

private String mNames[] = {
"内衣","外套","裤衩",
"男鞋","女鞋","text",
"name","type","search","logcat",
"杨超傻逼","杨超傻逼","杨超傻逼"
,"杨超傻逼","杨超傻逼"
};

private void initChildViews() {
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.leftMargin = 10;
lp.rightMargin = 10;
lp.topMargin = 5;
lp.bottomMargin = 5;
for (int i = 0; i < mNames.length; i++) {
TextView view = new TextView(this);
view.setText(mNames[i]);
view.setTextColor(Color.WHITE);

final int finalI = i;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,mNames[finalI], Toast.LENGTH_SHORT).show();
}
});
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
mFlowLayout.addView(view, lp);
}
}

public void add(View view) {
String n = name.getText().toString();
int i = dao.insertJson(n);

btn.setVisibility(View.VISIBLE);

List<String> sel3 = dao.sel();
a.add(0,n);
ArrayAdapter<String> adapter3 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, a);
lv.setAdapter(adapter3);
}

public void delall(View view) {
dao.del();
List<String> sel2 = dao.sel();
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, sel2);

lv.setAdapter(adapter2);

Toast.makeText(MainActivity.this,"清除成功",Toast.LENGTH_LONG).show();
btn.setVisibility(View.INVISIBLE);
}
}


//创建数据库

public class Myhelpher extends SQLiteOpenHelper{
public Myhelpher(Context context){
super(context,"sss.db",null,2);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建表
sqLiteDatabase.execSQL("create table shuju1(id integer primary key autoincrement,json text not null)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}


//插入数据操作Dao

public class Dao {
private Myhelpher my;
private SQLiteDatabase db;
private SQLiteDatabase d;

public Dao(Context context) {
my=  new Myhelpher(context);
}

/**
* 插入数据的操作
*/
public int insertJson(String json){

SQLiteDatabase database = my.getWritableDatabase();
//再去添加
ContentValues values = new ContentValues();
values.put("json",json);
database.insert("shuju1",null,values);
//关闭
database.close();
return 1;
}

public List<String> sel(){
d = my.getReadableDatabase();

List<String> list=new ArrayList<>();
Cursor cursor = d.rawQuery("select * from shuju1", null);

while (cursor.moveToNext()){
String s = cursor.getString(1);
list.add(s);
}
return list;
}

public void del(){
db = my.getWritableDatabase();
db.execSQL("delete from shuju1");
}

public int delyi(String i){
db = my.getWritableDatabase();
db.execSQL("delete from shuju1 where json=?",new String[]{i});
return 1;
}
}


//历史搜索布局

public class liushi extends ViewGroup{
//存储所有子View
private List<List<View>> mAllChildViews = new ArrayList<>();
//每一行的高度
private List<Integer> mLineHeight = new ArrayList<>();

public liushi(Context context) {
super(context);

}

public liushi(Context context, AttributeSet attrs) {
super(context, attrs);

}

public liushi(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//父控件传进来的宽度和高度以及对应的测量模式
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 b, int l, int i1, int i2, int i3) {
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;
}
}

/**
159.     * 与当前ViewGroup对应的LayoutParams
160.     */
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//Toast.makeText(getContext(),mAllChildViews.toString(), Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}


//shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#666666" />
<corners android:radius="10dp" />
<padding
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="5dp" />
</shape>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: