如何在ViewPager中的数组中传递Arraylist <uri>插入的整数

时间:2019-06-25 15:26:03

标签: android arraylist android-viewpager

我正在尝试在View pager中从SD卡加载图像。我有uri的arrayList。如何更改我的代码以打开这些图像而不是可绘制对象。

我的mainAvtivity班

class NilPortugues\Foundation\Domain\Model\Repository\Page#942 (7) {
  protected $elements =>
  class SplFixedArray#941 (0) {
  }
  protected $totalPages =>
  int(1)
  protected $totalElements =>
  int(0)
  protected $pageNumber =>
  int(1)
  protected $sort =>
  class NilPortugues\Foundation\Domain\Model\Repository\Sort#938 (2) {
    protected $properties =>
    NULL
    protected $order =>
    NULL
  }
  protected $filter =>
  class NilPortugues\Foundation\Domain\Model\Repository\Filter#953 (1) {
    protected $filters =>
    NULL
  }
  protected $fields =>
  class NilPortugues\Foundation\Domain\Model\Repository\Fields#940 (1) {
    private $fields =>
    array(0) {
    }
  }
}

1 个答案:

答案 0 :(得分:1)

假设您有ArrayList<Uri> uriList。现在,像这样更改代码:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager);
        mCustomPageAdapter = new CustomPageAdapter(this);
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(mCustomPageAdapter);
    }
    public class CustomPageAdapter extends PagerAdapter {
        Context mContext;
        LayoutInflater mLayoutInflater;
        public CustomPageAdapter(Context context) {
            mContext = context;
            mLayoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return uriList.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((LinearLayout) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.pager_item,container, false);
            ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView);
            imageView.setImageURI((uriList.get(position));
            container.addView(itemView);
            return itemView;
        }

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