我有一个 ViewPager 和 Spinner 的活动,我想做的就是通过选择 Spinner 中的项目来更改所有页面内容,以便更充分地理解我将解释 - 我正在开发一个简单的电视时间表每周节目,所以pages 是每日电视节目,而 spinner 是电视频道,当我们启动节目时,它将显示所有频道的简短 4 项节目列表,当我们选择“CNN”或微调器中的任何其他内容时,它将显示页面显示的一天的完整电视节目列表(用于显示我使用 TitlePageIndicator 的天),所以我的问题是当我在微调器中选择了一个项目,加载了数据并
调用了 notifydatasetchanged()对于我的 FragmentStatePagerAdapter - 它不会刷新页面。我没有明白我做错了什么?
我的适配器
private class TVProgrammFragmentAdapter extends FragmentStatePagerAdapter {
private FragmentManager fm;
private ArrayList<TVProgrammFragment> mFragments;
public TVProgrammFragmentAdapter(FragmentManager fm, ArrayList<TVProgrammFragment> mFragments) {
super(fm);
this.fm = fm;
this.mFragments = mFragments;
}
public CharSequence getPageTitle(int position) {
return ((TVProgrammFragment) mFragments.get(position)).getTitle();
}
public Fragment getItem(int arg0) {
return mFragments.get(arg0);
}
public int getCount() {
return mFragments.size();
}
// I don't know wherefore this line
// It is a first issue that I've googled
// And it's useless
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
MyActivity onCreate 中的一些代码
mAdapter = new TVProgrammFragmentAdapter(getSupportFragmentManager(), new ArrayList<TVProgrammFragment>());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
mIndicator.setTopPadding(0);
mIndicator.setSelectedBold(true);
mIndicator.setViewPager(mPager);
mSpinner = (Spinner) findViewById(R.id.spinner);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (mChannelIndex != position) {
String param = position == 0 ? String.valueOf(mTVProgrammAdapter.dates.get(mPager.getCurrentItem()).date) : String.valueOf(mTVProgrammAdapter.dates.get(mPager.getCurrentItem()).date) + "/" + String.valueOf(mTVProgrammAdapter.channels.get(position).id);
//Problems are here -->
setParamsToPages(param);
//<--
mChannelIndex = position;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
getTVProgrammDatesChannels();
这就是我在应用启动后获取数据的方式
//LoadHelper it is a class which does all data-loading things from server API
//And it works perfectly with any problems
LoadHelper.getTVProgrammDatesAndChannels(new OnLoadFinishedListener() {
@Override
public void onLoadFinished(final Object obj) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mTVProgrammAdapter = ((TVProgrammAdapter) obj);
mTVProgrammAdapter.channels.add(0, new TVProgrammChannel("-1", ALL_CHANNELS));
mTVChannelsAdapter = new ChannelsAdapter(mTVProgrammAdapter.channels);
mSpinner.setAdapter(mTVChannelsAdapter);
//Setting a pages for a dates
for (TVProgrammDate date : mTVProgrammAdapter.dates) {
TVProgrammFragment tvFragmemnt = new TVProgrammFragment();
tvFragmemnt.setArguments(date);
((TVProgrammFragmentAdapter) mPager.getAdapter()).mFragments.add(tvFragmemnt);
}
((TVProgrammFragmentAdapter) mPager.getAdapter()).notifyDataSetChanged();
mViewFlipper.setDisplayedChild(1);
}
});
}
这是我的问题 - 是一个 setParamsToPages 方法
public void setParamsToPages(final String param) {
new Thread(new Runnable() {
@Override
public void run() {
final ArrayList<TVProgrammFragment> fragments = ((TVProgrammFragmentAdapter) mPager.getAdapter()).mFragments;
//Here I setting a new fragments with new params to
//TVProgrammFragmentAdapter fragments ArrayList(mFragments)
for (TVProgrammFragment tvFragmemnt : fragments) {
TVProgrammFragment newFragment = new TVProgrammFragment();
String title = tvFragmemnt.getTitle();
newFragment.setArguments(title, param);
fragments.set(fragments.indexOf(tvFragmemnt), newFragment);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//doesn't do anything
mAdapter.notifyDataSetChanged();
//mAdapter still displaying an "old"
//fragments
}
});
}
});
}