ViewPager + Picasso

问题描述 投票:5回答:2

我正在使用Picasso + Viewpager创建一个Image Slider。 我可以根据图像大小(计数)滑动滑块。 但是我没有加载任何图像,我可以刷屏幕。 我的代码出了什么问题?

在我的MainActivity中,我获得了图像的链接。 然后像这样设置适配器。

 viewPager = (ViewPager)findViewById(R.id.view_pager);
        viewPagerAdapter = new ViewPagerAdapter(MainActivity.this,IMAGES);
        viewPager.setAdapter(viewPagerAdapter);  

IMAGES =链接数组(适用于图像)。

我的ImagePager_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:background="@drawable/plusbtn"/>
</RelativeLayout>

在这里我宣布了我的ViewPager ..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000">

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

并为我的适配器..

package com.thesis.juandirection.picasso;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.support.v4.view.ViewPager;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Galvez on 11/9/2015.
 */
public class ViewPagerAdapter extends PagerAdapter {
    private  Context context;
    private ArrayList<String> IMAGES = new ArrayList<>();


    public ViewPagerAdapter(Context context, ArrayList<String> IMAGES) {
        this.IMAGES = IMAGES;
        this.context = context;
    }

    @Override
    public int getCount() {
        return IMAGES.size();
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }


    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.imagepager_layout,null);
        ((ViewPager) collection).addView(view);
        final ImageView img = (ImageView) view.findViewById(R.id.img);
        Picasso.with(context)
                .load(IMAGES.get(position))
                .placeholder(R.drawable.plusbtn)
                .centerCrop()
                .fit()
                .into(img);
        return view;
    }
}
android android-studio imageview picasso
2个回答
5
投票

你应该改变这个;

@Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }

对此;

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

2
投票

正如@cemtufekci建议的那样,你必须相应地改变isViewFromObject。但如果您没有在<uses-permission android:name="android.permission.INTERNET" />中声明适当的AndroidManifest.xml权限,它将无法解决图像加载问题。

© www.soinside.com 2019 - 2024. All rights reserved.