我有一个FragmentActivity
由一个ViewPager
和一个组成的ActionBar
。这ViewPager
有一个带有 3 个片段的适配器。这些片段会TextView
根据在ActionBar
.
这是我的问题:每当我在 中的视图之间滑动时ViewPager
,我希望根据在ActionBar
. 我通过这样设置一个监听器来尝试这个:
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0)
{
setTimePeriod(actionBarItemPosition);
}
}
然后,setTimePeriod()
调用两个模拟方法:getComparedUsage()
和getThisUsage()
。这些中的每一个都对 CPU 负载很重,因此我的UI在视图之间滑动时非常滞后。
我该如何解决这个问题?
private void setTimePeriod(int position)
{
periodDropDownPosition = position;
// Get a reference to the active fragment currently shown by the ViewPager.
currentFragment = (DetailedFragment) mMyFragmentPagerAdapter.getItem(mViewPager.getCurrentItem());
List<UsageEntry> dataSet = currentFragment.getDataSet();
setViewReferencessOnActiveFragment(currentFragment);
differenceTextView = (TextView) activeView.findViewById(R.id.usage_results_total_value_tv);
try
{
switch(position)
{
case PERIOD_DAY:
resultsThisTextView.setText(R.string.compared_to_text_this_day);
resultsComparedToTextView.setText(R.string.compared_to_text_compared_day);
comparedUsage = getComparedUsage(PERIOD_DAY, dataSet);
// This call brings the phone to its knees
thisUsage = getThisUsage(PERIOD_DAY, dataSet);
// This call brings the phone to its knees
break;
case PERIOD_WEEK:
resultsThisTextView.setText(R.string.compared_to_text_this_week);
resultsComparedToTextView.setText(R.string.compared_to_text_compared_week);
comparedUsage = getComparedUsage(PERIOD_WEEK, dataSet);
// This call brings the phone to its knees
thisUsage = getThisUsage(PERIOD_WEEK, dataSet);
// This call brings the phone to its knees
break;
case PERIOD_MONTH:
resultsThisTextView.setText(R.string.compared_to_text_this_month);
resultsComparedToTextView.setText(R.string.compared_to_text_compared_month);
comparedUsage = getComparedUsage(PERIOD_MONTH, dataSet);
// This call brings the phone to its knees
thisUsage = getThisUsage(PERIOD_MONTH, dataSet);
// This call brings the phone to its knees
break;
case PERIOD_YEAR:
resultsThisTextView.setText(R.string.compared_to_text_this_year);
resultsComparedToTextView.setText(R.string.compared_to_text_compared_year);
comparedUsage = getComparedUsage(PERIOD_YEAR, dataSet);
// This call brings the phone to its knees
thisUsage = getThisUsage(PERIOD_YEAR, dataSet);
// This call brings the phone to its knees
break;
}
setCashOrUnits(isInCashMode);
}catch(NotEnoughDataException e)
{
noDataLayout = (RelativeLayout)activeView.findViewById(R.id.results_not_enough_data_view);
noDataLayout.setVisibility(View.VISIBLE);
resultsView = (RelativeLayout)activeView.findViewById(R.id.results_view);
resultsView.setVisibility(View.INVISIBLE);
e.printStackTrace();
}catch(NullPointerException e)
{
noDataLayout = (RelativeLayout)activeView.findViewById(R.id.results_not_enough_data_view);
noDataLayout.setVisibility(View.VISIBLE);
resultsView = (RelativeLayout)activeView.findViewById(R.id.results_view);
resultsView.setVisibility(View.INVISIBLE);
e.printStackTrace();
}
}