实现商品的搜索功能

//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>

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我来为您简单介绍一下这些功能实现方式: 1. 实现用户注册功能:在前端页面上,需要提供一个注册表单,用户填写完信息后点击提交按钮,前端将表单数据发送到后端接口进行处理,后端将用户信息保存至数据库中。 2. 实现用户登录功能:在前端页面上,需要提供一个登录表单,用户填写完账号密码后点击提交按钮,前端将表单数据发送到后端接口进行验证,后端将验证结果返回给前端,前端根据结果进行相应的处理。 3. 实现购物车功能:在前端页面上,需要提供一个购物车页面,用户可以在商品列表中选择需要购买的商品,点击添加至购物车按钮,前端将商品信息保存至本地存储中。用户在购物车页面中可以查看已添加的商品信息,修改商品数量、删除商品等操作,前端将修改后的购物车信息发送至后端接口进行处理。 4. 实现商品分类查询功能:在前端页面上,需要提供一个商品分类列表,用户可以选择所需的分类,前端将用户选择的分类信息发送至后端接口进行处理,后端返回符合条件的商品列表给前端进行展示。 5. 实现商品搜索功能:在前端页面上,需要提供一个搜索框,用户可以输入关键字进行搜索,前端将搜索关键字发送至后端接口进行处理,后端返回符合条件的商品列表给前端进行展示。 以上就是实现这些功能的基本方式,具体的实现过程和技术选型要根据具体的需求和情况进行选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值