阅读 1267

android一个仿照哔哩哔哩的搜索框框架,美观实用,拥有保留历史记录的功能?

建立测试前先新建一个moudle或者project用来测试,废话少说,先看一下功能效果: center

1.导入依赖包

1.导入依赖包

implementation 'com.wenwenwen888:searchbox:1.0.1'
复制代码

如果想了解这个框架的源码,可以去看这个地址github地址

2.框架代码的介绍

1.序列化

final SearchFragment searchFragment = SearchFragment.newInstance();
复制代码

2.回调,获取搜索数据

searchFragment.setOnSearchClickListener(new IOnSearchClickListener() {
@Override
public void OnSearchClick(String keyword) {
    //这里处理逻辑
    Toast.makeText(ToolBarActivity.this, keyword, Toast.LENGTH_SHORT).show();
}
});
复制代码

3.显示搜索框

searchFragment.showFragment(getSupportFragmentManager(),SearchFragment.TAG);
复制代码

3.框架的使用

MainActivity页面

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.wyt.searchbox.SearchFragment;
import com.wyt.searchbox.custom.IOnSearchClickListener;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SearchFragment searchFragment = SearchFragment.newInstance();
        searchFragment.setOnSearchClickListener(new IOnSearchClickListener() {
            @Override
            public void OnSearchClick(String keyword) {
                Intent intent=new Intent(MainActivity.this, MainActivity2.class);
                intent.putExtra("suo",keyword);
                startActivity(intent);
            }
        });
        imageView=findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchFragment.showFragment(getSupportFragmentManager(),SearchFragment.TAG);
            }
        });
    }
}
复制代码

acitivity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="25dp"
            android:layout_height="20dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="12dp"
            android:src="@drawable/search"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
文章分类
Android
文章标签