我有一个ViewPager和Spinner的活动,我想要做的就是通过选择Spinner中的项目来更改所有页面内容,以便更全面地理解我将解释 - 我正在开发一个简单的电视节目表每周programm,所以页面是每日电视节目和微调器是一个电视频道,当我们启动一个程序时,它将显示所有频道的短项目4项目列表,当我们选择“CNN”或其他任何一个微调器它将显示一个完整的电视节目列表,显示一天显示的页面(用于显示我使用TitlePageIndicator的日子),所以我的问题是当我在微调器中选择了一个项目,加载了数据并且有
我的FragmentStatePagerAdapter调用了一个notifydatasetchanged() - 它不会刷新页面。我没有得到我做错了什么?
MyAdapter
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
}
});
}
});
}
答案 0 :(得分:0)
我明白了!我忘了开始一个线程LOL!=)