使用来自url的图像填充ViewPager

时间:2017-02-08 12:27:38

标签: c# xamarin

首先,我按照指南here进行了实施。该指南使用静态资源从中获取图像。然而,我的应用程序执行HTTP调用并获取包含图像链接的JSON。我想将链接传递给ViewPager适配器,以便我可以使用Glide库填充它。有没有办法做到这一点?我没有链接我的代码,因为它是指南中使用不同名称的完整副本。

HTTP电话

public class JsonCallRequest
{
    public static async Task<JsonValue> CallJSON(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
        request.ContentType = "application/json";
        request.Method = "GET";

        using (HttpClient client = new HttpClient())
        {
            var response = await client.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                using (Stream stream = await response.Content.ReadAsStreamAsync())
                {
                    JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                    return jsonDoc;
                }
            }
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

在活动中呼叫服务器。从服务器返回结果后,我建议你读出jsonDoc并查看网址隐藏的位置,以及可以找到的参数

类似的东西:

public class test : Activity{
    Android.Support.V4.View.ViewPager viewPager;

    adapterTest adapter;

    public test(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) {
    }

    public test() {
    }

    protected override void OnCreate(Android.OS.Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);
        viewPager = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.viewpager);
        adapter = new adapterTest(this);
        performCall();
    }

    async void performCall() {
        // Perform cal here
        var result = await makeMyCall();

        // Figure out the structure of the result
        Console.WriteLine(result);

        // Sample of selecting the urls from result using Linq
        IEnumerable<string> urls = result.Select(x => x.url);
        adapter.Urls = urls;
        viewPager.Adapter = adapter;
    }
}

接下来你的自定义PagerAdapter看起来像这样:

public class adapterTest : Android.Support.V4.View.PagerAdapter {

    Context context;
    public adapterTest(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) {
    }

    public adapterTest() {
        this.context = context;
        Urls = new List<string>();
    }

    public IEnumerable<string> Urls { get; set; }

    public override int Count {
        get {
            return Urls.Count();
        }
    }

    public override bool IsViewFromObject(View view, Java.Lang.Object @object) {
        throw new NotImplementedException();
    }

    public override Java.Lang.Object InstantiateItem(ViewGroup container, int position) {
        var imageView = new ImageView(context);
        // load image with glide
        imageView.SetImageResource(Urls.ElementAt(position));
        var viewPager = container.JavaCast<ViewPager>();
        viewPager.AddView(imageView);
        return imageView;
    }
}

一旦从HTTP呼叫接收到信息,就在适配器中设置新属性Urls。接下来,设置viewpager的适配器,重新加载viewpager的所有视图。