如何从Android中的URL向ViewPager添加多个图像?

时间:2015-09-12 23:46:23

标签: java android android-viewpager

我希望在我正在制作的应用上使用ViewPager在我的网站上显示多个图像。我尝试了不同的方法,但却无法让它发挥作用。这是我的代码:

MainActivity.java:

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);
    }
}

ImageAdapter.java

package com.example.myapplication

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.URL;

public class ImageAdapter extends PagerAdapter {

    Context context;
    private static final int COUNT = 1;

    ImageAdapter(Context context) {
        this.context = context;
    }

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

    @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.padding_medium);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        new LoadImage(imageView).execute("http://ingridwu.dmmdmcfatter.com/wp-content/uploads/2015/01/placeholder.png");
        ((ViewPager) container).addView(imageView, 0);

        return imageView;
    }

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

    private class LoadImage extends AsyncTask<String, String, Bitmap> {
        ImageView img = null;

        public LoadImage(ImageView img) {
            this.img = img;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected Bitmap doInBackground(String... args) {
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0].getContent());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        protected void onPostExecute(Bitmap image) {
            if (image != null) {
                img.setImageBitmap(image);
            }
        }
    }
}

activity_main.xml中:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#333" >

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="360dp" />

</RelativeLayout>

就像我说的那样,我希望能够从URL添加多个图像。我甚至尝试设置COUNT = 2并创建一个新的LoadImage并将其添加到视图中就像我添加第一个图像但它设置了两个图像的第一个链接,然后,当我滑动时,它将两个图像更改为链接#2和那个它是如何保持的。任何和所有的帮助表示赞赏。谢谢!

0 个答案:

没有答案