本文介绍了寻呼机适配器从响应中的片段后得到不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到从反应的三维图像,而我存储在数组列表,然后我尝试PagerAdapter设置,但在我看来,寻呼机没有显示器,下面是我的pageradapter类,任何一个可以告诉大家,什么是错误,我做?

我使用的片段

在我Postexecute我设置它针对寻呼机这样

 适配器=新ImageAdapter(All_Product_Details.this.getActivity());
            viewPager.setAdapter(适配器);
}
 

下面是我的适配器,我AsyncTask的后置

 公共类ImageAdapter扩展FragmentPagerAdapter {
    上下文语境;
    ImageAdapter(上下文的背景下)
    {
        this.context =背景;
    }
    @覆盖
    公众诠释getCount将(){
        返回multimglist.size();
    }

    @覆盖
    公共片段的getItem(int i)以{
        返回null;
    }

    @覆盖
    公共无效destroyItem(View容器,INT位置,Object对象){
        ((ViewPager)容器).removeView((查看)对象);
    }
    @覆盖
    公共布尔isViewFromObject(查看视图,Object对象){
        返回查看==((ImageView的)对象);
    }

    @覆盖
    公共对象instantiateItem(ViewGroup中的容器,INT位置){
        ImageView的ImageView的=新ImageView的(上下文);
        INT填充= context.getResources()。getDimensionPixelSize(
                R.dimen.activity_horizo​​ntal_margin);
        imageView.setPadding(填充,填充,填充,填充);
        //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        Picasso.with(上下文).load(multimglist.get(位置))到(ImageView的)。


        // imageView.setImageURI(Uri.parse(imgStr [位置]));
        ((ViewPager)容器).addView(ImageView的,0);
        返回ImageView的;
    }
}
 

解决方案

如果您使用的是片段,你应该使用 FragmentPagerAdapter ,不是 PagerAdapter 。请参见官方文档这里。你应该重写的getItem(INT位置)返回所需的片段在适当的位置。

如果你的文档,这些都显示一个名为 FragmentPagerAdapter MyAdapter 。这看起来像:

 公共静态类MyAdapter扩展FragmentPagerAdapter {

    公共MyAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公众诠释getCount将(){
        返回NUM_ITEMS;
    }

    @覆盖
    公共片段的getItem(INT位置){
        返回ArrayListFragment.newInstance(位置);
    }
}
 

的getItem(INT位置)方法应返回特定的新实例片段您正在返回,这意味着什么也不会显示出来。例如,在我自己的code,我重写的getItem()是这样的:

  @覆盖
公共片段的getItem(INT位置)
{
    开关(位置)
    {
    案例PagerConstants.PAGE_FILTER_RECIPES:
        返回新FilteredRecipesFragment();

    案例PagerConstants.PAGE_SELECTED_RECIPES:
        返回新SelectedRecipesFragment();

    案例PagerConstants.PAGE_SHOPPING_LIST:
        返回新ShoppingListFragment();

    默认://确保这一从未发生过。
        返回null;
    }
}
 

I am getting three images from response,and i am storing in arraylist,then i try to set in PagerAdapter,but nothing display in my view pager,following is my pageradapter class,can any one tell that what is mistake i made?

I am using Fragment

In my Postexecute i am setting it in view pager like this

adapter = new ImageAdapter(All_Product_Details.this.getActivity());
            viewPager.setAdapter(adapter);
}

below is my adapter which i set after asynctask

      public class ImageAdapter extends FragmentPagerAdapter {
    Context context;
    ImageAdapter(Context context)
    {
        this.context=context;
    }
    @Override
    public int getCount() {
        return multimglist.size();
    }

    @Override
    public Fragment getItem(int i) {
        return null;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(
                R.dimen.activity_horizontal_margin);
        imageView.setPadding(padding, padding, padding, padding);
        //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        Picasso.with(context).load(multimglist.get(position)).into(imageView);


        // imageView.setImageURI(Uri.parse(imgStr[position]));
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }
}
解决方案

If you are using Fragments, you should use FragmentPagerAdapter, not PagerAdapter. See official documentation here. You should override getItem(int position) to return the desired Fragment at the proper position.

If you look at the documentation, they show a FragmentPagerAdapter class named MyAdapter. This looks like:

public static class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }
}

The getItem(int position) method should return a new instance of a particular Fragment. You are currently returning null, which means that nothing will be displayed. For example, in my own code, I override getItem() like this:

@Override
public Fragment getItem(int position)
{
    switch (position)
    {
    case PagerConstants.PAGE_FILTER_RECIPES:
        return new FilteredRecipesFragment();

    case PagerConstants.PAGE_SELECTED_RECIPES:
        return new SelectedRecipesFragment();

    case PagerConstants.PAGE_SHOPPING_LIST:
        return new ShoppingListFragment();

    default: // Make sure that this never happens.
        return null;
    }
}

这篇关于寻呼机适配器从响应中的片段后得到不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:08