我的活动构成了viewpager和fragment,但是当我第一次输入它时没有数据,如果我将寻呼机从一个滚动到四个,然后将四个滚动到两个,这一刻它将显示数据,然后你继续滚动,所有数据都会显示。我不知道为什么会这样,你能帮助我吗?以下是我的代码:
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);
}
}
答案 0 :(得分:1)
不要在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);
};
};
希望这会对你有所帮助。
答案 1 :(得分:0)
您的列表的适配器在mHandler中不相同。