Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有gridview的Viewpager片段_Android - Fatal编程技术网

Android 带有gridview的Viewpager片段

Android 带有gridview的Viewpager片段,android,Android,使用gridview创建片段的正确方法是什么? 我可以这样做吗? 我如何在运行时将适配器添加到gridview以向其中添加项 private void initialisePaging() { List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this, fragment1.class.getName())); frag

使用gridview创建片段的正确方法是什么? 我可以这样做吗?


我如何在运行时将适配器添加到gridview以向其中添加项

private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this, fragment2.class.getName()));
    fragments.add(Fragment.instantiate(this, fragment3.class.getName()));
    this.mPagerAdapter  = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
    //this.mPagerAdapter.setListener(this);
    Log.e("init", "message");
    pager = (ViewPager)super.findViewById(R.id.viewpager_menu);
    pager.setAdapter(this.mPagerAdapter);

    GridView gridView = (GridView)findViewById(R.layout.menu1_fragment);
}
private void initialisePaging(){
列表片段=新向量();
add(Fragment.instantiate(这是fragment1.class.getName());
add(Fragment.instantiate(这是fragment2.class.getName());
add(Fragment.instantiate(这是fragment3.class.getName());
this.mPagerAdapter=新的MyPagerAdapter(super.getSupportFragmentManager(),fragments);
//this.mPagerAdapter.setListener(this);
Log.e(“初始化”、“消息”);
pager=(ViewPager)super.findViewById(R.id.ViewPager\u菜单);
pager.setAdapter(this.mPagerAdapter);
GridView GridView=(GridView)findViewById(R.layout.menu1_片段);
}

当我这样做时,gridview为空,我无法将适配器连接到它。

您应该在
PagerAdapter
实例化Item
方法中查找/配置各个页面上的视图,而不是在配置寻呼机本身时。每个页面都是在需要时惰性地创建的,而
instanceItem
是为每个页面调用的设置。当您试图在此处找到您的
GridView
实例时,它还不存在,因为寻呼机还没有创建它,而且您还没有将它本地化到您要设置的页面。

我该如何获得GridView@重写公共对象实例化项(ViewGroup容器,int位置){Fragment g=fragments.get(position);GridView GridView=(GridView)g.getView();返回super.InstanceItem(容器,位置);}GridView仍然为空。
private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this, fragment2.class.getName()));
    fragments.add(Fragment.instantiate(this, fragment3.class.getName()));
    this.mPagerAdapter  = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
    //this.mPagerAdapter.setListener(this);
    Log.e("init", "message");
    pager = (ViewPager)super.findViewById(R.id.viewpager_menu);
    pager.setAdapter(this.mPagerAdapter);

    GridView gridView = (GridView)findViewById(R.layout.menu1_fragment);
}