My activity make up viewpager and fragment,but when I first enter it hasn't data,if I scroll the pager from one to four,and then scroll form four to two,this moment it will show data, and you continue scroll, the all data will show.I don't know why this is,can you help me ? The following is my code:
public class WatchVideosActivity extends FragmentActivity {
private TabPageIndicator mTabIndicator;
private ViewPager mViewPager;
private int mWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_videos);
getScreenWidth();
initView();
}
/**
* 获取屏幕的宽度
*/
private void getScreenWidth(){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mWidth = dm.widthPixels;
}
/**
* 初始化View
*/
private void initView(){
mTabIndicator = (TabPageIndicator)findViewById(R.id.tab_pager_indicator);
mViewPager = (ViewPager)findViewById(R.id.watch_video_viewpager);
mViewPager.setAdapter(new WatchVideoPagerAdapter(WatchVideosActivity.this,getSupportFragmentManager(),mWidth));
mTabIndicator.setViewPager(mViewPager);
}
}
public class WatchVideoPagerAdapter extends FragmentPagerAdapter{
public static final String[] title = new String[]{"期指在线","和讯座谈会","中国经济学人","中国分析师访谈"};
private Context mContext;
private int mWidth;
public WatchVideoPagerAdapter(Context context,FragmentManager fm,int width) {
super(fm);
this.mContext = context;
this.mWidth = width;
}
@Override
public int getCount() {
return title.length;
}
@Override
public Fragment getItem(int position) {
return new FragmentFactory(mContext,position,mWidth); //创建fragment
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
public class FragmentFactory extends Fragment {
private int position;
private ListView mWatchVideoList;
private Context mContext;
private int mWidth;
private Integer mPagerIndex = 1;
private Integer[] titleId = new Integer[] { 123817825, 180524655, 168567237, 182001231 };
private List<VideoEntity> newsPagerList = new ArrayList<VideoEntity>();
private VideoAdapter videoAdapter;
public FragmentFactory(Context context, int position, int width) {
super();
this.position = position;
this.mContext = context;
this.mWidth = width;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.watchvideo_fragment_view, container, false);
mWatchVideoList = (ListView) view.findViewById(R.id.watchvideo_list);
getData();
mWatchVideoList.setAdapter(videoAdapter);
return view;
}
private Handler mHandler = new Handler() {
@SuppressWarnings("unchecked")
public void handleMessage(android.os.Message msg) {
newsPagerList = (List<VideoEntity>) msg.obj;
videoAdapter = new VideoAdapter(mContext, mWatchVideoList, newsPagerList, mWidth);
videoAdapter.notifyDataSetChanged();
};
};
private void getData() {
OkHttpUtils.getInputStream("http://wapi.hexun.com/Api_videoList.cc?pid=" + titleId[position] + "&pc=20&pn=1",
mHandler, 0);
}
}
Do not instantiate VideoAdapter inside handleMessage() instead instantiate it for one time in onCreteView
}
Make a method inside VideoAdapter
When you getting the data from Server then call the addItems() method of adapter which will notify your list and you will show your data.
};
Hope this will help you.