我有一个带有两个片段的ViewPager,每个片段都有一个GridView
Frag1.java
public class Frag1 extends Fragment{
GridView grid;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag1, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
grid = (GridView) getActivity().findViewById(R.id.gridView1);
grid.setAdapter(new MyAdapter());
}
private class MyAdapter extends BaseAdapter {
private int ar []={R.drawable.ch1,R.drawable.ch2,R.drawable.ch3,R.drawable.ch4};
private LayoutInflater inflater;
@Override
public int getCount() {
return ar.length;
}
@Override
public Object getItem(int i) {
return ar[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ViewHolder holder = null;
if(v == null) {
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.grid_layout, viewGroup, false);
holder = new ViewHolder();
v.setTag(holder);
}else{
holder = (ViewHolder) v.getTag();
}
holder.img = (ImageView)v.findViewById(R.id.imageView1);
holder.img.setBackgroundResource(ar[i]);
return v;
}
private class ViewHolder{
ImageView img;
}
}
Frag2与Frag1相同,但有两个不同的东西
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag, container, false);
}
private class MyAdapter extends BaseAdapter {
private int ar []={R.drawable.gmc1,R.drawable.gmc2,R.drawable.gmc3,R.drawable.gmc4};
......
}
这是包含ViewPager
的SecondActivity@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
adapter = new MyPagerAdapter(fm);
pager.setAdapter(adapter);
}
class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment frag;
if (position == 0) {
frag = new Frag1();
} else {
frag = new Frag2();
}
return frag;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
问题是ViewPage只能查看一个页面(Frag2),滚动页面时会得到一个空白页面
答案 0 :(得分:1)
这很奇怪......
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
grid = (GridView) getActivity().findViewById(R.id.gridView1);
grid.setAdapter(new MyAdapter());
}
为什么要在Activity中查找gridView?可能你应该在onCreateView中执行此操作而不会弄乱onActivityCreated
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag, container, false);
grid = (GridView) rootView.findViewById(R.id.gridView1);
grid.setAdapter(new MyAdapter());
return rootView;
}
一些解释:
您正在致电
getActivity().findViewById(R.id.gridView1);
在其子图片(片段布局)中的ViewPager(以及它是扩展的ViewGroup)中的Activity中搜索。两者都调用gridView1,假设此网格仅附加到Frag1,您正在使用此视图创建Frag1并使用ch drawables设置Adapter。然后创建第二个片段Frag2,从早先在Frag1中膨胀的Activity GridView获取并仍然出现在屏幕上(alive,not null)并设置gmc adapter