切换选项卡时 SearchView 停止工作
SearchView stops working when switch tab
我在 TabLayout 中有 3 个片段,如此处所示 http://imgur.com/a/Z9dTQ
每当我启动该应用程序时,它都会打开资源管理器选项卡,我可以切换到搜索选项卡并且搜索视图完美运行。如果我切换到资源管理器并返回搜索,它仍然有效。
我的 SearchView 不使用可搜索的 activity,而是使用 onQueryTextChange 侦听器进行搜索。在 SearchView 下面是一个 LinearLayout,有 30 个 TextView,通过搜索改变文本。
但是,如果我切换到测验选项卡并返回搜索,SearchView 文本仍然存在,但 TextView 被重置为其默认值 "gone",如下所示:http://imgur.com/a/L4RvE。现在尝试使用 SearchView 进行搜索无效。
切换到测验选项卡似乎缩小了我的搜索布局,当我切换回来时,它扩大了搜索布局但没有正确实例化 SearchView。当我再次尝试搜索时,不会调用 OnQueryTextChange。我不明白为什么会这样。
当我向 QuizLayout.xml 添加 SearchView 并将第二个 SearchView 的代码添加到测验选项卡的 MainActivity 中的 onCreateOptionsMenu 时,问题就开始了。然而,即使在注释掉它并确保所有 ID 都正确之后,问题仍然存在。
下面是我的代码,省略了一些(希望)不相关的代码。
我的搜索片段:
package com.example.ryans45attcdie.chess;
public class SearchFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search_fragment, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
searchLayout =
(LinearLayout)getView().findViewById(R.id.searchLayoutS);
}
}
我的 QuizFragment:(为空调试)
public class QuizFragment extends Fragment {}
我的主要活动:
public class MainActivity extends AppCompatActivity {
TextView sname1;//(etc.)
ArrayList<TextView> masterNameListS;
List<Fragment> fragments = new Vector<>();
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up fragments in tabs
fragments.add(Fragment.instantiate(this, QuizFragment.class.getName()));
fragments.add(Fragment.instantiate(this,
ExplorerFragment.class.getName()));
fragments.add(Fragment.instantiate(this,
SearchFragment.class.getName()));
PagerAdaptor adaptor = new PagerAdaptor(getSupportFragmentManager(),
fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(adaptor);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
tabLayout.getTabAt(0).setText("QUIZ");
tabLayout.getTabAt(1).setText("EXPLORE");
tabLayout.getTabAt(2).setText("SEARCH");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.list_content, listItems);
pager.setCurrentItem(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
sname1 = (TextView) findViewById(R.id.sname1); //(etc.)
final ArrayList<TextView> masterNameListS = new ArrayList<>();
masterNameListS.add(sname1); //(etc.)
// Get the SearchView and set the searchable configuration
SearchManager searchManagerS = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
SearchView searchViewS = (SearchView) findViewById(R.id.searchViewS);
// Assumes current activity is the searchable activity
ComponentName cnS = new ComponentName(this, SearchableActivity.class);
searchViewS.setSearchableInfo(searchManagerS.getSearchableInfo(cnS));
searchViewS.setIconifiedByDefault(false); // Do not iconify the widget;
expand it by default
searchViewS.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String s) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
//parse search string and set TextView texts here
return true;
}
});
return true;
}
@Override
public booleafn 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
和 PagerAdapter:
package com.example.ryans45attcdie.chess;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.WindowManager;
import java.util.List;
public class PagerAdaptor extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdaptor(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return fragments.size();
}
}
我的 QuizLayout.xml 是空的。
我的 SearchLayout.xml 创建了所有没有文本和 Visibility.GONE.
的 TextView
非常感谢大家的帮助,
斯蒂芬
解决了:)
问题是默认情况下,ViewPager 在内存中只保留一个屏幕外选项卡。因为我的资源管理器选项卡首先打开,所以它存储在内存中。当我从搜索切换到测验时,我的 ViewPager 决定删除搜索选项卡,并保留资源管理器选项卡。
要解决此问题,请将 your_View_Pager_here.setOffscreenPageLimit(number_to_store);
放入 MainActivity onCreate 方法中。
我在 TabLayout 中有 3 个片段,如此处所示 http://imgur.com/a/Z9dTQ
每当我启动该应用程序时,它都会打开资源管理器选项卡,我可以切换到搜索选项卡并且搜索视图完美运行。如果我切换到资源管理器并返回搜索,它仍然有效。
我的 SearchView 不使用可搜索的 activity,而是使用 onQueryTextChange 侦听器进行搜索。在 SearchView 下面是一个 LinearLayout,有 30 个 TextView,通过搜索改变文本。
但是,如果我切换到测验选项卡并返回搜索,SearchView 文本仍然存在,但 TextView 被重置为其默认值 "gone",如下所示:http://imgur.com/a/L4RvE。现在尝试使用 SearchView 进行搜索无效。
切换到测验选项卡似乎缩小了我的搜索布局,当我切换回来时,它扩大了搜索布局但没有正确实例化 SearchView。当我再次尝试搜索时,不会调用 OnQueryTextChange。我不明白为什么会这样。
当我向 QuizLayout.xml 添加 SearchView 并将第二个 SearchView 的代码添加到测验选项卡的 MainActivity 中的 onCreateOptionsMenu 时,问题就开始了。然而,即使在注释掉它并确保所有 ID 都正确之后,问题仍然存在。
下面是我的代码,省略了一些(希望)不相关的代码。
我的搜索片段:
package com.example.ryans45attcdie.chess;
public class SearchFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search_fragment, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
searchLayout =
(LinearLayout)getView().findViewById(R.id.searchLayoutS);
}
}
我的 QuizFragment:(为空调试)
public class QuizFragment extends Fragment {}
我的主要活动:
public class MainActivity extends AppCompatActivity {
TextView sname1;//(etc.)
ArrayList<TextView> masterNameListS;
List<Fragment> fragments = new Vector<>();
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up fragments in tabs
fragments.add(Fragment.instantiate(this, QuizFragment.class.getName()));
fragments.add(Fragment.instantiate(this,
ExplorerFragment.class.getName()));
fragments.add(Fragment.instantiate(this,
SearchFragment.class.getName()));
PagerAdaptor adaptor = new PagerAdaptor(getSupportFragmentManager(),
fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(adaptor);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
tabLayout.getTabAt(0).setText("QUIZ");
tabLayout.getTabAt(1).setText("EXPLORE");
tabLayout.getTabAt(2).setText("SEARCH");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.list_content, listItems);
pager.setCurrentItem(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
sname1 = (TextView) findViewById(R.id.sname1); //(etc.)
final ArrayList<TextView> masterNameListS = new ArrayList<>();
masterNameListS.add(sname1); //(etc.)
// Get the SearchView and set the searchable configuration
SearchManager searchManagerS = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
SearchView searchViewS = (SearchView) findViewById(R.id.searchViewS);
// Assumes current activity is the searchable activity
ComponentName cnS = new ComponentName(this, SearchableActivity.class);
searchViewS.setSearchableInfo(searchManagerS.getSearchableInfo(cnS));
searchViewS.setIconifiedByDefault(false); // Do not iconify the widget;
expand it by default
searchViewS.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String s) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
//parse search string and set TextView texts here
return true;
}
});
return true;
}
@Override
public booleafn 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
和 PagerAdapter:
package com.example.ryans45attcdie.chess;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.WindowManager;
import java.util.List;
public class PagerAdaptor extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdaptor(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return fragments.size();
}
}
我的 QuizLayout.xml 是空的。 我的 SearchLayout.xml 创建了所有没有文本和 Visibility.GONE.
的 TextView非常感谢大家的帮助,
斯蒂芬
解决了:)
问题是默认情况下,ViewPager 在内存中只保留一个屏幕外选项卡。因为我的资源管理器选项卡首先打开,所以它存储在内存中。当我从搜索切换到测验时,我的 ViewPager 决定删除搜索选项卡,并保留资源管理器选项卡。
要解决此问题,请将 your_View_Pager_here.setOffscreenPageLimit(number_to_store);
放入 MainActivity onCreate 方法中。