我有3個片段在TabLayout這裏http://imgur.com/a/Z9dTQ搜索查看停止工作時切換標籤
看到每當我啓動應用程序,它會打開資源管理器選項卡,我可以切換到搜索標籤和搜索查看完美的作品。如果我切換到資源管理器並返回搜索,它仍然有效。
我搜索查看不使用搜索的活動,而是與onQueryTextChange監聽器搜索。下面的搜索查看是30個TextViews其文本通過搜索被改變的LinearLayout。
但是,如果我切換到測驗選項卡並返回搜索,SearchView文本仍然存在,但TextView被重置爲其「默認值」的默認值,如下所示:http://imgur.com/a/L4RvE。嘗試使用SearchView進行搜索不起作用。
看來,切換到測驗片放氣我的搜索佈局,當我再次打開,它膨脹的搜索佈局,但不能正確實例化搜索查看。當我嘗試再次搜索時,不調用OnQueryTextChange。我無法弄清楚爲什麼這樣做。
問題開始時,我增加了一個搜索查看到QuizLayout.xml,並添加代碼,第二個搜索查看到onCreateOptionsMenu在MainActivity的測驗標籤。但即使在評論出來並確保所有ID都是正確的之後,問題仍然存在。
下面是我的代碼,有一些(希望)無關的代碼省略。
我SearchFragment:
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 {}
我的MainActivity:
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的TextViews。
非常感謝幫助傢伙,
斯蒂芬