防京东搜索页面


//依赖

apply plugin: 'org.greenrobot.greendao'
greendao {
    schemaVersion 1
    daoPackage 'com.example.jingdongdeom'
    targetGenDir 'src/main/java'
}
compile 'org.greenrobot:greendao:3.0.0'



classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'





代码


package com.example.jingdongdeom.Activity;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jingdongdeom.Bean.User;
import com.example.jingdongdeom.Dao.DaoMaster;
import com.example.jingdongdeom.Dao.DaoSession;
import com.example.jingdongdeom.Liushi.XCFlowLayout;
import com.example.jingdongdeom.R;
import com.example.jingdongdeom.Dao.UserDao;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;

public class SerachActivity extends AppCompatActivity {


//定义数组
            private String mNames[] = {
                    "男装","电脑","手机","apple",
                    "坚果","苹果",
                    "戴尔","阿迪短袖","迪凯斯男装",
                   "男士皮鞋"
           };


            private EditText editText;
            private XRecyclerView xRecyclerView;
            private UserDao userDao;
            private User user;
            private String sname;
            private List<String> list = new ArrayList<>();
            private ListView lv;
            private Adapter adapter;
            private XCFlowLayout mFlowLayout;
             private ArrayList<String> list2 = new ArrayList<>();
            private String string;
    private ImageView back3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serach);
                editText = findViewById(R.id.edname);
                 back3 = findViewById(R.id.back3);

                 back3.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View view) {
                         finish();
                     }
                 });


                lv = findViewById(R.id.lv);

                initChildViews();
                //调取Greendao
                DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"user_db");
                SQLiteDatabase database = helper.getReadableDatabase();
                DaoMaster daoMaster = new DaoMaster(database);
                DaoSession session = daoMaster.newSession();

                userDao = session.getUserDao();

                //长按删除
                lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                        userDao.deleteByKey((long) i);
                        list.remove(i);
                     //   adapter.notifyDataSetChanged();
                        lv.setAdapter(new Adapter(SerachActivity.this,list));
                        return false;
                    }
                });

                //点击传值到搜索框
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        editText.setText(list.get(i).toString());

                    }
                });

                //查询数据库
                List<User> users = userDao.loadAll();
                if (users!=null){

                    for (int i=0;i<users.size();i++){

                        String sname = users.get(i).getSname();
                        list.add(sname);
                    }
                    lv.setAdapter(new Adapter(SerachActivity.this,list));
                }
            }
            //搜索,添加数据库
            public void serach(View view) {

                sname = editText.getText().toString();


                //跳转展示页面
                if(sname!=null){

                    Intent intent  = new Intent(SerachActivity.this,ZhanShiActivity.class);
                    intent.putExtra("name",sname);

                    startActivity(intent);
                }
                else{


                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                }

                //将数据添加到数据库
                user = new User(null, sname);
                long insert = userDao.insert(user);
                list.add(sname);
                adapter = new Adapter(SerachActivity.this, list);
                lv.setAdapter(adapter);

            }
            //删除
            public void delete(View view) {

                userDao.deleteAll();

                list.clear();
                adapter = new Adapter(SerachActivity.this, list);
                lv.setAdapter(adapter);

            }

    //listview适配器
            class Adapter extends BaseAdapter{
                Context context;
                List<String> list;
                public Adapter(Context context, List<String> list) {
                    this.context = context;
                    this.list = list;
                }
                @Override
                public int getCount() {
                    return list.size();
                }

                @Override
                public Object getItem(int i) {
                    return list.get(i);
                }

                @Override
                public long getItemId(int i) {
                    return i;
                }

                @Override
                public View getView(int i, View view, ViewGroup viewGroup) {
                    TextView textView = new TextView(context);
                    textView.setText(list.get(i));
                    textView.setTextSize(20);

                    return textView;
                }
            }
            //流式布局
            private void initChildViews() {

                mFlowLayout = findViewById(R.id.flowlayout);
                 ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(

                     ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.leftMargin = 5;
                lp.rightMargin = 5;
                lp.topMargin = 5;
                lp.bottomMargin = 5;
                for( int i = 0; i < mNames.length; i ++){
                    final TextView view1 = new TextView(this);
                    view1.setText(mNames[i]);
                    view1.setTextColor(Color.RED);
                    view1.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
                    mFlowLayout.addView(view1,lp);

                    view1.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {

                           String lname = view1.getText().toString();

                           //Toast.makeText(SerachActivity.this,view1.getText().toString() , Toast.LENGTH_SHORT).show();
                           Intent intent = new Intent(SerachActivity.this,ZhanShiActivity.class);
                           intent.putExtra("name",lname);
                           startActivity(intent);
                       }
                   });
                }


            }

        }

//布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.jingdongdeom.Activity.SerachActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/line">

        <ImageView
            android:id="@+id/back3"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="35dp"
            android:background="@drawable/leftjiantou"
            />

    <EditText
        android:singleLine="true"
        android:id="@+id/edname"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_weight="4"
        android:hint="请输入搜索内容"
        />

        <Button
            android:onClick="serach"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:text="搜索"
            android:background="#f89"
            />

    </LinearLayout>

    <com.example.jingdongdeom.Liushi.XCFlowLayout
        android:id="@+id/flowlayout"
        android:layout_width="match_parent"

        android:layout_height="100dp" >

    </com.example.jingdongdeom.Liushi.XCFlowLayout>


    <TextView

        android:id="@+id/jilu"
        android:layout_below="@id/line"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:text="历史记录"
        android:gravity="center"
        />


    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv"
        android:layout_below="@id/jilu">

    </ListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



    <Button
        android:onClick="delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="全部清除"
        android:layout_alignParentBottom="true"
        />
    </RelativeLayout>




</LinearLayout>
//流式布局

package com.example.jingdongdeom.Liushi;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huoxuebin on 2018/4/26.
 */

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

    public XCFlowLayout(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }
    public XCFlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        // TODO Auto-generated constructor stub
    }
    public XCFlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub

        //父控件传进来的宽度和高度以及对应的测量模式
        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 changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        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;
        }

    }
    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub

        return new MarginLayoutParams(getContext(), attrs);
    }
}

package com.example.jingdongdeom.Bean;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by huoxuebin on 2018/4/22.
 */
@Entity
public class User {
    @Id
    private Long id;
    private String sname;
    public String getSname() {
        return this.sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Generated(hash = 405242272)
    public User(Long id, String sname) {
        this.id = id;
        this.sname = sname;
    }
    @Generated(hash = 586692638)
    public User() {
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值