这是带有 viewpager 的 searchview 的简单示例
接口IDataCallback
import java.util.ArrayList;
public interface IDataCallback {
void onFragmentCreated( ArrayList<String> listData);
}
接口IFragmentListener
public interface IFragmentListener {
void addiSearch(ISearch iSearch);
void removeISearch(ISearch iSearch);
}
界面搜索
public interface ISearch {
void onTextQuery(String text);
}
你的活动MainActivity
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, SearchView.OnQueryTextListener, IFragmentListener {
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
ArrayList<ISearch> iSearch = new ArrayList<>();
private MenuItem searchMenuItem;
private String newText;
private PageAdapter adapter;
ArrayList<String> listData = null;
IDataCallback iDataCallback = null;
public void setiDataCallback(IDataCallback iDataCallback) {
this.iDataCallback = iDataCallback;
iDataCallback.onFragmentCreated(listData);
}
@Override
public void addiSearch(ISearch iSearch) {
this.iSearch.add(iSearch);
}
@Override
public void removeISearch(ISearch iSearch) {
this.iSearch.remove(iSearch);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listData = new ArrayList<>();
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
adapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), newText);
//Adding adapter to pager
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
public void getDataFromFragment_one(ArrayList<String> listData) {
this.listData = listData;
Log.e("-->", "" + listData.toString());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public boolean onQueryTextChange(String newText) {
this.newText = newText;
adapter.setTextQueryChanged(newText);
for (ISearch iSearchLocal : this.iSearch)
iSearchLocal.onTextQuery(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
主要活动 xml activity_main
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
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">
<!-- our toolbar -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
页面适配器 PageAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PageAdapter extends FragmentStatePagerAdapter {
private String mSearchTerm;
//integer to count number of tabs
int tabCount;
//Constructor to the class
public PageAdapter(FragmentManager fm, int tabCount, String searchTerm) {
super(fm);
//Initializing tab count
this.tabCount= tabCount;
this.mSearchTerm= searchTerm;
}
//Overriding method getItem
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
Tab1 tab1 = Tab1.newInstance(mSearchTerm);
return tab1;
case 1:
Tab2 tab2 = Tab2.newInstance(mSearchTerm);
return tab2;
case 2:
Tab3 tab3 = Tab3.newInstance(mSearchTerm);
return tab3;
default:
return null;
}
}
//Overriden method getCount to get the number of tabs
@Override
public int getCount() {
return tabCount;
}
public void setTextQueryChanged(String newText) {
mSearchTerm = newText;
}
}
第一个片段Tab1
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class Tab1 extends Fragment implements ISearch {
private static final String ARG_SEARCHTERM = "search_term";
private String mSearchTerm = null;
ArrayList<String> strings = null;
private IFragmentListener mIFragmentListener = null;
ArrayAdapter<String> arrayAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
view = inflater.inflate(R.layout.tab1, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview1);
strings = new ArrayList<>();
for (int i = 0; i < 20; i++) {
strings.add(String.valueOf(i));
}
strings.add("11");
arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, strings);
listView.setAdapter(arrayAdapter);
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.getDataFromFragment_one(strings);
if (getArguments() != null) {
mSearchTerm = (String) getArguments().get(ARG_SEARCHTERM);
}
return view;
}
@Override
public void onResume() {
super.onResume();
if (null != mSearchTerm) {
onTextQuery(mSearchTerm);
}
}
public Tab1() {
}
public static Tab1 newInstance(String searchTerm) {
Tab1 fragment = new Tab1();
Bundle bundle = new Bundle();
bundle.putString(ARG_SEARCHTERM, searchTerm);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onTextQuery(String text) {
arrayAdapter.getFilter().filter(text);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIFragmentListener = (IFragmentListener) context;
mIFragmentListener.addiSearch(Tab1.this);
}
@Override
public void onDetach() {
super.onDetach();
if (null != mIFragmentListener)
mIFragmentListener.removeISearch(Tab1.this);
}
}
第一个片段 xml tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview1">
</ListView>
</RelativeLayout>
第二个片段Tab2
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab2 extends Fragment implements ISearch {
private static final String ARG_SEARCHTERM = "search_term";
private String mSearchTerm = null;
private IFragmentListener mIFragmentListener = null;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
View view = inflater.inflate(R.layout.tab2, container, false);
if(getArguments()!=null) {
mSearchTerm = (String) getArguments().get(ARG_SEARCHTERM);
}
return view;
}
@Override
public void onTextQuery(String text) {
}
@Override
public void onResume() {
super.onResume();
if(null!=mSearchTerm) {
onTextQuery(mSearchTerm);
}
}
public static Tab2 newInstance(String searchTerm){
Tab2 fragment = new Tab2();
Bundle bundle = new Bundle();
bundle.putString(ARG_SEARCHTERM, searchTerm);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIFragmentListener = (IFragmentListener) context;
mIFragmentListener.addiSearch(Tab2.this);
}
@Override
public void onDetach() {
super.onDetach();
if(null!=mIFragmentListener)
mIFragmentListener.removeISearch(Tab2.this);
}
}
第二个片段 xml tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 2"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
第三个片段Tab3
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
//Our class extending fragment
public class Tab3 extends Fragment implements ISearch, IDataCallback {
private static final String ARG_SEARCHTERM = "search_term";
private String mSearchTerm = null;
ArrayList<String> strings = null;
private IFragmentListener mIFragmentListener = null;
ArrayAdapter<String> arrayAdapter = null;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
view = inflater.inflate(R.layout.tab3, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview2);
((MainActivity) getActivity()).setiDataCallback(this);
arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, strings);
listView.setAdapter(arrayAdapter);
if (getArguments() != null) {
mSearchTerm = (String) getArguments().get(ARG_SEARCHTERM);
}
return view;
}
@Override
public void onTextQuery(String text) {
arrayAdapter.getFilter().filter(text);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onResume() {
super.onResume();
if (null != mSearchTerm) {
onTextQuery(mSearchTerm);
}
}
public static Tab3 newInstance(String searchTerm) {
Tab3 fragment = new Tab3();
Bundle bundle = new Bundle();
bundle.putString(ARG_SEARCHTERM, searchTerm);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIFragmentListener = (IFragmentListener) context;
mIFragmentListener.addiSearch(Tab3.this);
}
@Override
public void onDetach() {
super.onDetach();
if (null != mIFragmentListener)
mIFragmentListener.removeISearch(Tab3.this);
}
@Override
public void onFragmentCreated(ArrayList<String> listData) {
strings = listData;
}
}
第三个片段 xml tab3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview2">
</ListView>
</RelativeLayout>
活动menu.xml的菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
yourapp:showAsAction="collapseActionView|always"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>