ViewPager+fragment用 FrameLayout(针布局)做每个fragment的返回的view,结果第一页不显示数据,但 当切换回第一页时,就加载出来了,不知道那里有问题?

simoluanbu 发布于 2016/12/21 14:04
阅读 224
收藏 0
ViewPager+fragment用 FrameLayout(针布局)做每个fragment的返回的view,结果第一页不显示数据,但
当切换回第一页时,就加载出来了,不知道那里有问题?
打日志后个人想法:
1.手动初始化第一个页面BaseFragment fragment = FragmentFactory.createFragment(0);
fragment.loadData();没有调用BaseFragment的loadData(),因为针布据LoadingPager


没new出来,即还没走完BaseFragment里的OnCreateView方法。。。。不知为何。。。。

2.当不使用缓冲布局时,直接加载数据,可以显示第一页

代码如下:
public class HomeActivity extends FragmentActivity {


private RadioButton rb_movie;
private RadioButton rb_cinema;
private RadioButton rb_find;
private RadioButton rb_mine;
private ViewPager vp_content;
private RadioGroup rg_group;
private MyAdapter myAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.activity_home);


initView();


myAdapter = new MyAdapter(getSupportFragmentManager());
vp_content.setAdapter(myAdapter);
vp_content.setCurrentItem(0);

//这里的数据没加载,调用loadData()方法时,还没走BaseFragment的oncreateView(


)方法,没初始化针布局。。。
BaseFragment fragment = FragmentFactory.createFragment(0);
fragment.loadData();

// 当vp页面发生改变时,改变RadioButton的选中状态,加载fragment数据
vp_content.setOnPageChangeListener(new OnPageChangeListener() {




@Override
public void onPageSelected(int arg0) {

BaseFragment fragment = FragmentFactory.createFragment(arg0);
fragment.loadData();// TODO 这里加载数据可以,节省流量

System.out.println("arg"+arg0);

switch (arg0) {
case 0:
rb_movie.setChecked(true);
break;
case 1:
rb_cinema.setChecked(true);
break;
case 2:
rb_find.setChecked(true);
break;
case 3:
rb_mine.setChecked(true);
break;


default:
break;
}

}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}


@Override
public void onPageScrollStateChanged(int arg0) {
}
});

rb_movie.setChecked(true);



// 当RadioButton选中时,设置跳到当前的fragment
rg_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_movie:
vp_content.setCurrentItem(0);
break;
case R.id.rb_cinema:
vp_content.setCurrentItem(1);


break;
case R.id.rb_find:
vp_content.setCurrentItem(2);


break;
case R.id.rb_mine:
vp_content.setCurrentItem(3);


break;


}
}
});



}


private void initView() {
vp_content = (ViewPager) findViewById(R.id.vp_content);


rg_group = (RadioGroup) findViewById(R.id.rg_group);


rb_movie = (RadioButton) findViewById(R.id.rb_movie);
rb_cinema = (RadioButton) findViewById(R.id.rb_cinema);
rb_find = (RadioButton) findViewById(R.id.rb_find);
rb_mine = (RadioButton) findViewById(R.id.rb_mine);


}



class MyAdapter extends FragmentPagerAdapter {


private String[] stringArrays;


public MyAdapter(FragmentManager fm) {
super(fm);
stringArrays = UIUtils.getStringArray(R.array.tab_names);

}
@Override
public Fragment getItem(int arg0) {
return FragmentFactory.createFragment(arg0);
}


@Override
public int getCount() {
return stringArrays.length;
}


}
}



加载中
返回顶部
顶部