为什么第一次输入我的 activity 组成 viewpager + fragment 时没有数据?

why didn't have data when the first enter my activity it make up viewpager + fragment?

我的activity组成了viewpager和fragment,但是我第一次进入的时候没有数据,如果我把pager从1滚动到4,然后从4滚动到2,这时候就可以了显示数据,然后继续滚动,所有数据将 show.I 不知道这是为什么,你能帮帮我吗?以下是我的代码:

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);
}

}

您的列表适配器在 mHandler 中不相同。

不要在 handleMessage() 中实例化 VideoAdapter 而是在 onCreteView 中实例化一次

@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);
 newsPagerList=new ArrayList<VideoEntity>();
videoAdapter = new VideoAdapter(mContext, mWatchVideoList, newsPagerList, mWidth); ///Send empty list for the first time
mWatchVideoList.setAdapter(videoAdapter);
getData();
return view;

}

在VideoAdapter中创建一个方法

VideoAdapter{


public void addItems(ArrayList<VideoEntity> list){
 newsPageList.addAll(list); ///newsPageList is your list used in Adapter
 notifyDataSetChanged();
}  

当您从服务器获取数据时,然后调用适配器的 addItems() 方法,它会通知您的列表并显示您的数据。

private Handler mHandler = new Handler() {
@SuppressWarnings("unchecked")
public void handleMessage(android.os.Message msg) {
    newsPagerList = (List<VideoEntity>) msg.obj;
    videoAdapter.addItems(newsPagerList);
};

};

希望对您有所帮助。