我想在SearchView
的工具栏上实施Navigation-Drawer
,图片中有Fragment Activity
。我已经在常规SearchView
上尝试了Activity
并且我成功了,但是当我将其实施到Fragment Activity
时,为什么它的代码无法运行。
这是我的Main Activity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public static final String MAKAN = "makanan";
public static String WHERE = "makanan";
public static int WHERE2;
String mQuery;
ArrayList<Makanan> mlist = new ArrayList<>();
ArrayList<Makanan> mlistAll = new ArrayList<>();
public String mQuery2;
boolean isFiltered;
MakananAdapter mAdapter;
ArrayList<Integer> mListMapFilter = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = ((NavigationView)findViewById(R.id.nav_view)).getHeaderView(0);
ImageButton ib = (ImageButton)header.findViewById(R.id.imageButton);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
startActivity(intent);
}
});
setTitle("Beranda");
Hal1 fragment = new Hal1();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment, "fragment1");
//WHERE = "11";
fragmentTransaction.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
setTitle("About");
Hal2 fragments = new Hal2();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragments, "fragment1");
fragmentTransaction.commit();
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.frag_all) {
setTitle("Rekomendasi");
SecondFragment fragment = new SecondFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment, "fragment1");
WHERE = "11";
fragmentTransaction.commit();
} ...
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}}
我的SecondFragment.java
public class SecondFragment extends Fragment implements MakananAdapter.IMakananAdapter, SearchView.OnQueryTextListener {
public static final String MAKAN = "makanan";
private AppCompatActivity compat;
private RecyclerView recyclerView;
private MakananAdapter adapter;
public SecondFragment() {
// Required empty public constructor
}
ArrayList<Makanan> mList = new ArrayList<>();
//MakananAdapter mAdapter;
String mQuery;
ArrayList<Makanan> mlist = new ArrayList<>();
ArrayList<Makanan> mlistAll = new ArrayList<>();
boolean isFiltered;
MakananAdapter mAdapter;
ArrayList<Integer> mListMapFilter = new ArrayList<>();
private SearchView searchView = null;
private SearchView.OnQueryTextListener queryTextListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_calling, container, false);
recyclerView = (RecyclerView) rootview.findViewById(R.id.recyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
recyclerView .setHasFixedSize(true);
recyclerView .setItemAnimator(new DefaultItemAnimator());
recyclerView .setLayoutManager(gridLayoutManager);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new MakananAdapter(mList);
recyclerView.setAdapter(adapter);
return rootview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
fillData1();
//if i use this call method, the function is work
//[enter image description here][1]doFilter("roker");
}
private void fillData1() {
Resources resources = getResources();
String[] arJudul = resources.getStringArray(R.array.recommend);
String[] arDeskripsi = resources.getStringArray(R.array.recommend_desc);
String[] arHarga = resources.getStringArray(R.array.recommend_price);
String[] arDetail = resources.getStringArray(R.array.recommend_details);
String[] arLokasi = resources.getStringArray(R.array.recommend_locations);
String[] arLat = resources.getStringArray(R.array.recommend_lat);
String[] arLng = resources.getStringArray(R.array.recommend_longi);
TypedArray a = resources.obtainTypedArray(R.array.recommend_pict);
String[] arFoto = new String[a.length()];
for (int i = 0; i < arFoto.length; i++) {
int id = a.getResourceId(i, 0);
arFoto[i] = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ resources.getResourcePackageName(id) + '/'
+ resources.getResourceTypeName(id) + '/'
+ resources.getResourceEntryName(id) + '/';
}
a.recycle();
for (int i = 0; i < arJudul.length; i++) {
mList.add(new Makanan(arJudul[i], arDeskripsi[i], arFoto[i], arHarga[i], arDetail[i], arLokasi[i], arLat[i], arLng[i]));
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint("Cari");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Log.i("onQueryTextSubmit", query);
//mQuery = query.toLowerCase();
//doFilter(mQuery);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
mQuery = newText.toLowerCase();
doFilter(mQuery);
return true;
}
});
return;
}
//My filter function goes here
public boolean doFilter(String query) {
if (!isFiltered) {
mlistAll.clear();
mlistAll.addAll(mList);
isFiltered = true;
}
mList.clear();
if (query.isEmpty()) {
mList.addAll(mlistAll);
isFiltered = false;
return false;
} else {
mListMapFilter.clear();
for (int i = 0; i < mlistAll.size(); i++) {
Makanan makanan = mlistAll.get(i);
if (makanan.judul.toLowerCase().contains(query) ||
makanan.deskripsi.toLowerCase().contains(query)) {
mList.add(makanan);
mListMapFilter.add(i);
}
}
return true;
}
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
@Override
public void doClick(int pos) {
Intent intent = new Intent(getContext(), DetailActivity.class);
intent.putExtra(MAKAN, mList.get(pos));
startActivity(intent);
}}
所以,我的问题是如何通过用户输入查询来运行doFilter
函数?那可能吗 ?提前谢谢
图片链接 [1]:https://ibb.co/fFO3gQ - doFilter不起作用 [2]:https://ibb.co/dYXw1Q - 静态doFilter工作
答案 0 :(得分:0)
假设您已将searchable.xml
添加到res/xml/
项目目录和AndroidManifest.xml
,您已将MainActivity
声明为searchable
活动。
1。将menu.xml
添加到包含使用res/menu
SearchView
的菜单项的app:actionViewClass="android.support.v7.widget.SearchView"
文件夹中
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/search_hint"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
2。设置SearchView
并将SearchView.setOnQueryTextListener
添加到MainActivity
。
3。 Ony在您的Filter
课程中添加静态Fragment
功能。例如:
public static void doFilter(String searchQuery) {
...........
....................
}
更新MainActivity
,如下所示:
public class MainActivity extends AppCompatActivity {
Context mContext;
// Default active navigation menu
int mActiveMenu;
// TAGS
public static final int MENU_FIRST = 0;
public static final int MENU_SECOND = 1;
// Action bar search widget
SearchView searchView;
String searchQuery = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
................
...........................
// FirstFragment will show when app launch
mActiveMenu = MENU_FIRST;
displayViewWithFragment(mActiveMenu);
// NavigationView
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId())
{
case R.id.navigation_item_first:
{
// First
displayViewWithFragment(MENU_FIRST);
return true;
}
case R.id.navigation_item_second:
{
// Second
displayViewWithFragment(MENU_SECOND);
return true;
}
default:
return true;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Getting search action from action bar and setting up search view
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView)searchItem.getActionView();
// Setup searchView
setupSearchView(searchItem);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar actions click
switch (item.getItemId())
{
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Search widget
private void setupSearchView(MenuItem searchItem)
{
SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null)
{
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText)
{
searchQuery = newText;
// Load search data on respective fragment
if(mActiveMenu == MENU_FIRST) // First
{
FirstFragment.doFilter(newText);
}
else if(mActiveMenu == MENU_SECOND) // Second
{
SecondFragment.doFilter(newText);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
//searchView.clearFocus();
return false;
}
});
// Handling focus change of search view
searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// Focus changed after pressing back key or pressing done in keyboard
if (!hasFocus) {
searchQuery = "";
}
}
});
}
/**
* Displaying fragment view for selected nav drawer list item
* */
private void displayViewWithFragment(final int position) {
// Active menu
mActiveMenu = position;
Fragment fragment = null;
switch (position)
{
case MENU_FIRST:
{
// First
fragment = new FirstFragment();
}
break;
case MENU_SECOND:
{
// Second
fragment = new SecondFragment();
}
break;
default:
break;
}
if (fragment != null)
{
mFragmentTransition = getSupportFragmentManager().beginTransaction();
mFragmentTransition.replace(R.id.frame_container, fragment);
mFragmentTransition.commit();
}
}
}
将filter
功能添加到FirstFragment
:
public static void doFilter(String searchQuery)
{
// Filter codes here
...................
.............................
// Update RecyclerView
adapter.notifyDataSetChanged();
}
希望这会有所帮助〜