我一直在玩com.example.android.common/view/SlidingTabLayout个例子,并且遇到了一个小问题。我知道如何解决/解决它,但我正在努力理解这个问题。
我已经创建了一个带有六个标签的小应用程序,显示我想要的wot,通常它的行为与预期一致,但是如果我快速浏览选项卡之间,按下标签标题,那么我点击后退按钮创建一个新选项卡标题视图,并将其附加到populateTabStrip()中的mTabStrip,从而生成一组标签标题。问题只发生在导航是快速的时候,以悠闲的方式点击标签并且问题没有出现,所以假设它与垃圾收集有关,请参阅下文。无论如何,想知道是否有人能解释幕后发生的事情。
来自SlidingTabBasicFragment.java的
:
private void createAdapters(View view){
Log.v(TAG, "createAdapters()");
try {
//TODO createAdapters() work out why the app's ghosting
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
int currentItem = mViewPager.getCurrentItem();
SamplePagerAdapter testAdaptor = (SamplePagerAdapter)mViewPager.getAdapter();
if ( testAdaptor == null ) {
Log.v(TAG, "createAdapters() - new SamplePagerAdapter() to be added");
mViewPager.setAdapter(new SamplePagerAdapter());
}
mViewPager.setCurrentItem(currentItem);
SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
rebuildViews = false;
} catch (NullPointerException e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
来自主要活动:
@Override
public void onNavigationDrawerItemSelected(int position, int lastPosition) {
Log.v(TAG, "onNavigationDrawerItemSelected - entered");
Intent intent;
String createdFileName;
// update the main content by replacing fragments
FragmentTransaction transaction = getFragmentManager().beginTransaction();
onSectionAttached(position + 1);
switch (position) {
case 0: // Main - Vitals
Log.v(TAG, "adding a 'vitalFrag' to the fragment stack");
transaction.replace(R.id.main_content_fragment, new SlidingTabsBasicFragment(), "vitalFrag").addToBackStack(null).commit();
break;
case 1: // Settings
...
@Override
public void onBackPressed(){
Log.v(TAG, "onBackPressed - entered");
// Catch back action and pops from backStack
// (if you called previously to addToBackStack() in your transaction)
if (getFragmentManager().getBackStackEntryCount() > 0){
// If Open, close the Navigation Drawer, as can be confusing if the Navigation
// Drawer shows a different Option / Fragment as selected, after the previous
// fragment has been restored
NavigationDrawerFragment mNavFrag = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
if (mNavFrag != null && mNavFrag.isVisible() && mNavFrag.isDrawerOpen()) {
mNavFrag.closeDrawer();
mClosedDrawer = true;
}
// Pull back the last fragment
getFragmentManager().popBackStack();
}
// Default action on back pressed
else super.onBackPressed();
}
@Override
public void onVitalChanged() {
Log.v(TAG, "onVitalChanged() entered");
requestBackup();
//TODO onVitalChanged() - finish - just invalidate the views
SlidingTabsBasicFragment fragment = (SlidingTabsBasicFragment) getFragmentManager().findFragmentByTag("vitalFrag");
if (fragment != null) {
//Log.v(TAG, "onVitalChanged() asking fragment to rebuild the Vital views");
//fragment.setRebuildViews();
Log.v(TAG, "onVitalChanged - replace the Results fragment");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content_fragment, new SlidingTabsBasicFragment(), "vitalFrag")
.addToBackStack(null)
.commitAllowingStateLoss();
}
}
来自其中一个片段:
....
@Override
public void onResume() {
Log.v(TAG, "onResume() - entered");
if (rebuildViews) {
invalidateViews(this.getView());
}
ActionBar mActionBar = getActivity().getActionBar();
if (mActionBar != null)
mActionBar.setTitle(R.string.action_history);
super.onResume();
}