当前位置: 首页 > 知识库问答 >
问题:

在ViewPager android中创建片段

贾成天
2023-03-14

MainActivity(扩展了fragmentActivity)有listview,它调用listviewadapter,其中有一个带有ViewpagerAdapter的ViewPager。现在,如果我需要在ViewPager中生成5个片段,如何将getFragmentManager()传递给适配器。

MainActivity[fragmentActivity]---->Listview---->ListViewAdapter[自定义BaseAdapterAdapter与布局ViewPager的getview一起]-->ViewPager-->ViewPageAdapter[我需要根据位置创建5个片段?]

如果我粘贴整个代码,那么它会太长,所以我只附加了

public class ViewPagerAdapter extends PagerAdapter  {    
    ArrayList<ModelClass> arrayModelClasses = new ArrayList<ModelClass>();
    Context mcontext ;
    LinearLayout layouttest;

    final int PAGE_COUNT = 5;
    private String titles[] = new String[] { "Villa ","", "" ,"","",""};

    private int[] imageResId = { R.drawable.transparant,R.drawable.ic_action_place, R.drawable.icon_3d1,
            R.drawable.icon_flooplan,R.drawable.icon_gallery,R.drawable.icon_location };

    @SuppressLint("NewApi")
    @Override
    public void finishUpdate(ViewGroup container) {
        // TODO Auto-generated method stub
        super.finishUpdate(container);
    }

    public ViewPagerAdapter(ArrayList<ModelClass> arrayModelClasses , Context context) {
        super();
        this.arrayModelClasses = arrayModelClasses;
        this.mcontext= context;
    }

    @Override
    public boolean isViewFromObject(View collection, Object object) {
        return collection == ((View) object);
    }

    View view;

    @Override
    public Object instantiateItem(View collection, int position) {
        if (position == 0) {
            //need to call fragment 1
        }if (position == 1) {
        //need to call fragment 2
        }if (position == 2) {
        //need to call fragment 3
        }if (position == 3) {
        //need to call fragment 4
        }if (position == 4) {
        //need to call fragment 5
        }   
        return view;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
         Drawable image = mcontext.getResources().getDrawable(imageResId[position]);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            SpannableString sb = new SpannableString(" " +  titles[position] );
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return sb;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

}

链接,但它使用FragmentPagerAdapter为viewPager并从FragmentActivity传递getSupportFragmentManager,但这是不可能的2个适配器,所以帮助。

共有1个答案

戚均
2023-03-14

考虑到您没有发布ListView适配器,我假设您扩展了BaseAdapter(无论哪种方式都是相似的)。在GetView(...)方法上,实例化一个新的ViewPagerAdapter并将其设置为列表项XML中定义PagerAdapter

注意:在listview中包含viewpager(特别是在以这种方式加载项时)可能会导致性能低下。

 类似资料:
  • YQL(简化): 结果(简化): 如何告诉Vespa从“计算机”而不是“de”创建代码段?

  • 我试图在Java创建的布局中创建一个地图(GoogleMap)。我不明白为什么在尝试从LinearLayout获取ID时会出现与imageView相关的错误。 这是我的代码: 这是我得到的日志:

  • 注意:要通过 Photoshop CS5 创建 Web 画廊,请参阅 Adobe Bridge 帮助中的创建 Web 照片画廊。要使用下面主题中描述的早期可选 Web 照片画廊增效工具,请首先下载并安装适用于 Windows 或 Mac OS 的增效工具。 关于 Web 照片画廊 Web 照片画廊是一个 Web 站点,它具有一个包含缩览图图像的主页和若干包含完整大小图像的画廊页。每页都包含链接,使

  • 此时,Frag2在堆栈上,是唯一可见的片段。我使用了replace和addToBackStack,因为我需要后退导航。我的问题是,当我在Frag2内旋转屏幕时,活动中的super.oncreate(savedInstanceState)方法调用frag1的构造函数。 在用户按下后退按钮之前,有没有什么方法可以避免调用Frag1的构造函数?

  • 从各个组件创建一个业务网络卡片。创建业务网络卡片时,你将需要一个enrollSecret或一对 certificate和privateKey。 composer card create --file conga.card --businessNetworkName penguin-network --connectionProfileFile connection.json --user cong

  • 我使用FragmentActivity在片段之间切换。但我希望在片段上有一个管理按钮,当我单击它时,一个新的片段或活动看起来像一个孩子(在操作栏中有后退按钮)。 我怎样才能做到? 这是我的代码,可以使用,但“后退”按钮不会出现在操作栏中: 片段: 活动(目前……但如果需要,可能会出现碎片?): 也许我必须更改舱单中的某些内容?