流式布局+搜索框+数据库

本文展示了如何在Android应用中实现流式布局,集成搜索框,并与数据库交互。使用了包括BottomTabBar、OkHttp、Gson、RxJava、Retrofit、Glide等库。同时,详细介绍了布局XML文件`activity_main.xml`的结构,以及`MainActivity`和`Main2Activity`的Java代码,涉及SQLite数据库操作,包括创建表、插入、删除和查询数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

implementation 'com.hjm:BottomTabBar:1.1.1'
    //okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    //gson
    implementation 'com.google.code.gson:gson:2.6.2'
    //流式布局
    implementation 'com.fynn.fluidlayout:fluidlayout:1.0'
    implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.7'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
    implementation 'com.xhb:xbanner:1.2.2'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
// 一般依赖:
    implementation 'com.facebook.fresco:fresco:0.14.1'
    // 如果需要支持gif,再添加:
//依赖
implementation 'com.hjm:BottomTabBar:1.1.1'
    //okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    //gson
    implementation 'com.google.code.gson:gson:2.6.2'
    //流式布局
    implementation 'com.fynn.fluidlayout:fluidlayout:1.0'
    implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.7'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
    implementation 'com.xhb:xbanner:1.2.2'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
// 一般依赖:
    implementation 'com.facebook.fresco:fresco:0.14.1'
    // 如果需要支持gif,再添加:
    implementation 'com.facebook.fresco:animated-gif:0.12.0'
    // 在 API < 14 上的机器支持 WebP 时,需要添加
    implementation 'com.facebook.fresco:animated-base-support:0.12.0'
    // 支持 WebP (静态图+动图),需要添加
    implementation 'com.facebook.fresco:animated-webp:0.12.0'
    implementation 'com.facebook.fresco:webpsupport:0.12.0'
    // 仅支持 WebP 静态图,需要添加
    implementation 'com.facebook.fresco:webpsupport:0.12.0'

    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.2'
    implementation 'com.youth.banner:banner:1.4.9'
    implementation 'org.greenrobot:eventbus:3.0.0'
    implementation files('libs/open_sdk_r5990_lite.jar')
    implementation 'com.fynn.fluidlayout:fluidlayout:1.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
//activity_main.xml
<FrameLayout 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:id="@+id/fg"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:id="@+id/f1_erweima"
            android:layout_width="60dp"
            android:layout_height="wrap_content">

            <ImageView

                android:id="@+id/image_sao"
                android:background="#ffffff"
                android:src="@drawable/sao_hei"
                android:layout_width="24dp"
                android:layout_height="20dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textColor="#ffffff"
                android:text="扫啊扫"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ssk"
            android:layout_marginLeft="3px"
            android:layout_marginRight="3px"
            android:background="@drawable/home_title_bar_search_corner_bg"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="40dp">

            <ImageView
                android:layout_marginLeft="5dp"
                android:src="@drawable/a_4"
                android:layout_width="20dp"
                android:layout_height="20dp" />

            <TextView
                android:id="@+id/sousuo"
                android:layout_marginLeft="5dp"
                android:gravity="center_vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:textSize="15dp"
                android:text="运动户外超级品牌类日"
                />
            <ImageView
                android:layout_marginRight="5dp"
                android:src="@drawable/root"
                android:layout_width="20dp"
                android:layout_height="20dp" />
        </LinearLayout>

        <LinearLayout
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:layout_width="50dp"
            android:layout_height="wrap_content">

            <ImageView
                android:background="#ffffff"
                android:src="@drawable/a9v"
                android:layout_width="20dp"
                android:layout_height="20dp" />
            <TextView
                android:textColor="#ffffff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:text="消息"/>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>
//MainActivity
package bwei.com.uuuuu0531;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView sousuo;
    private LinearLayout ssk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        sousuo = (TextView) findViewById(R.id.sousuo);
        ssk = (LinearLayout) findViewById(R.id.ssk);
        sousuo.setOnClickListener(this);
        ssk.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.ssk:

                break;
            case R.id.sousuo:
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                break;
        }
    }

}
//Sql
//MySql
package bwei.com.uuuuu0531.SQl;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by mac on 2018/4/17.
 */

public class MySql extends SQLiteOpenHelper {

    public MySql(Context context){
        super(context, "search.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String sql = "create table search(id integer primary key autoincrement,name varchar(20))";
        sqLiteDatabase.execSQL(sql);
    }

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

    }
}
//MySqlDao
package bwei.com.uuuuu0531.SQl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

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

/**
 * Created by mac on 2018/4/17.
 */

public class MySqlDao {
    private MySql mySQlite;
    private SQLiteDatabase db;

    public MySqlDao(Context context){
        mySQlite = new MySql(context);
        db = mySQlite.getReadableDatabase();
    }

    public void add(String name){
        ContentValues values = new ContentValues();
        values.put("name",name);
        db.insert("search",null,values);
    }

    public void delete(String name){
        db.delete("search","name=?",new String[]{name});
    }

    public void deleteAll(){
        db.execSQL("DELETE FROM search");
    }

    public List<String> select(){
        Cursor cursor = db.rawQuery("select * from search",null);
        List<String> list = new ArrayList<>();

        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            list.add(name);
        }

        return list;
    }

}
//activity_main2.xml
<?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=".Main2Activity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:gravity="center_vertical">

        <RadioButton
            android:id="@+id/bt_quxiao"
            android:layout_width="52dp"
            android:layout_height="wrap_content"
            android:text="取消"
            android:button="@null"
            android:textColor="#999999"
            android:gravity="center"/>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="1">

            <EditText
                android:id="@+id/search_edit2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/search_2"
                android:hint="        雷士京东超级品牌日, 2件8折"
                android:textSize="10sp"
                android:singleLine="true"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/search_2"
                android:layout_alignBottom="@id/search_edit2"
                android:layout_alignLeft="@id/search_edit2"
                android:layout_alignTop="@id/search_edit2"
                android:layout_marginLeft="10dp"/>
        </RelativeLayout>

        <RadioButton
            android:id="@+id/bt_search"
            android:layout_width="52dp"
            android:layout_height="wrap_content"
            android:text="搜索"
            android:button="@null"
            android:textColor="#999999"
            android:gravity="center"/>
    </LinearLayout>

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#cccccc"
        android:layout_marginTop="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="热搜"
        android:textColor="#000"
        android:layout_margin="10dp"/>

    <com.fynn.fluidlayout.FluidLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fl">
    </com.fynn.fluidlayout.FluidLayout>

    <ListView
        android:id="@+id/search_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

    <RadioButton
        android:id="@+id/rb_clear"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:button="@null"
        android:background="@drawable/button_clean"
        android:textColor="#999999"
        android:text="清空历史搜索"/>

</LinearLayout>
//Main2Activity
package bwei.com.uuuuu0531;

import android.content.Intent;
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.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.fynn.fluidlayout.FluidLayout;

import java.util.List;

import bwei.com.uuuuu0531.SQl.MySqlDao;

public class Main2Activity extends AppCompatActivity {

    MySqlDao mySQliteDao;
    EditText et;
    FluidLayout fluidLayout;
    Button bt_quxiao, bt_search;
    RadioButton rb_clear;
    ListView lv;
    List<String> list;
    MyAdapter myAdapter;

    String[] arrs = {
            "倩女幽魂", "单机斗地主", "天堂战记", "妖精的尾巴", "极限挑战", "我们相爱吧", "倚天屠龙记",
            "明星大侦探", "丰乳肥臀", "大主宰", "盗墓笔记", "鬼吹灯", "盘龙", "完美世界", "柠檬初上", "WIFI",
            "锁屏", "异术超能", "东方不败", "巅峰战舰", "小说", "污"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        et = findViewById(R.id.search_edit2);
        bt_quxiao = findViewById(R.id.bt_quxiao);
        bt_search = findViewById(R.id.bt_search);
        rb_clear = findViewById(R.id.rb_clear);
        fluidLayout = findViewById(R.id.fl);
        lv = findViewById(R.id.search_lv);
//配置热搜流式布局
        for (int i = 0; i < arrs.length; i++) {
            String ss = arrs[i];
            TextView textView = new TextView(Main2Activity.this);
            textView.setText(ss);
            textView.setTextSize(13);
            textView.setBackgroundColor(Color.parseColor("#f5f5f5"));

            FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(150, 50);
            params.setMargins(12, 12, 12, 12);

            fluidLayout.addView(textView, params);
        }

        //查询数据库,配置适配器,显示历史记录listview
        mySQliteDao = new MySqlDao(Main2Activity.this);
        list = mySQliteDao.select();
        myAdapter = new MyAdapter();
        lv.setAdapter(myAdapter);


        //点击事件,返回首页
        bt_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        //点击事件,添加搜索内容到数据库
        bt_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySQliteDao.add(et.getText().toString());

                list = mySQliteDao.select();
                myAdapter.notifyDataSetChanged();


                Intent intent = new Intent(Main2Activity.this,shopActivity.class);
                startActivity(intent);
            }
        });
        //点击事件,清空数据库中的搜索内容
        rb_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySQliteDao.deleteAll();

                list.removeAll(list);
                myAdapter.notifyDataSetChanged();
            }
        });

    }

    //显示搜索内容listview的适配器
    class MyAdapter extends BaseAdapter {

        @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(Main2Activity.this);
            textView.setText(list.get(i));
            textView.setPadding(15, 15, 15, 15);
            return textView;
        }
    }

}


//创建ShopActivity点击进入页面







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值