Create a new moudle or project to test before creating a test.
1. Import the dependency package
1. Import the dependency package
implementation 'com. Wenwenwen888: searchbox: 1.0.1'
Copy the code
If you want to see the source of the framework, you can go to github.
2. Introduction of framework code
1. The serialization
final SearchFragment searchFragment = SearchFragment.newInstance();
Copy the code
2. Callback to obtain the search data
searchFragment.setOnSearchClickListener(new IOnSearchClickListener() {
@Override
public void OnSearchClick(String keyword) {
// Handle logic here
Toast.makeText(ToolBarActivity.this, keyword, Toast.LENGTH_SHORT).show(); }});Copy the code
3. The search box is displayed
searchFragment.showFragment(getSupportFragmentManager(),SearchFragment.TAG);
Copy the code
3. Use of frameworks
MainActivity page
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); }}); }}Copy the code
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>
Copy the code