RecyclerView - 从网上加载图片

时间:2017-06-28 10:53:04

标签: android android-asynctask android-recyclerview picasso

我正在尝试制作一个小应用程序,它可以获取狗的随机图片和电影,并在循环视图中显示它们。我正在使用Picasso来加载来自URL的图片

我想要它的设计方式是每张图片都自己显示(占据整个回收者视图),当用户向上或向下滚动时,他分别看到上一张或下一张图片。

目前,我只使用AsyncTask从https://random.dog/woof.json抓取10张图片,将它们加载到ArrayList并将数据移动到适配器。目前没有按预期工作(多个图像进入我想要只展示一个的空间,并且由于某些奇怪的原因,我的AsyncTask被重复调用,即使我只是从我的MainActivity中调用它)

适配器代码:

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.DogViewHolder> {

public ArrayList<String> urlList = new ArrayList<>();
public Context mContext;


public RecycleViewAdapter(Context context) {
    mContext = context;
}


@Override
public DogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    int layoutForListItems = R.layout.list_items;
    LayoutInflater inflater =LayoutInflater.from(context);
    View view = inflater.inflate(layoutForListItems, null);
    return new DogViewHolder(view);
}

@Override
public void onBindViewHolder(final DogViewHolder holder, int position) {
    String url = null;
    if (urlList != null) {
        url = urlList.get(position);
    }
    if (url != null) {
        String imageType = url.substring(url.length() - 4);

        View v = new View(mContext);

        Log.d("##############", "IMAGE TYPE - " + imageType);

        switch (imageType) {
            case ".mp4":
                Log.d("########", "movie url - " + url);
                holder.mVidView.setVideoURI(Uri.parse(url));
                holder.mVidView.setVisibility(View.VISIBLE);
                holder.mVidView.requestFocus();
                holder.mVidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        holder.mVidView.start();
                    }
                });
                holder.mVidView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        Log.d("########", "Error playing movie...");
                        return false;
                    }
                });
                break;
            default:
                Picasso.with(mContext).load(url).into(holder.mImgView);
                holder.mImgView.setVisibility(View.VISIBLE);

        }
    }
    else {
        Log.d("###############", "url was null");
        holder.mImgView.setImageResource(R.drawable.no_image_found);
        holder.mImgView.setVisibility(View.VISIBLE);
    }


}
@Override
public int getItemCount() {
    if (urlList == null) { return 0; }
    return urlList.size();
}

public void updateData(ArrayList<String> newList) {
    urlList.addAll(newList);
    notifyDataSetChanged();
}

public class DogViewHolder extends RecyclerView.ViewHolder {

    private final VideoView mVidView;
    private final ImageView mImgView;

    private DogViewHolder(View v) {
        super(v);
        mVidView = (VideoView) v.findViewById(R.id.video_view);
        mImgView = (ImageView) v.findViewById(R.id.image_view);

    }
}

}

AsyncTask代码:(在OnCreate中调用)

public class AsyncTaskThingy extends AsyncTask<Void, Void, ArrayList<String>> {


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

    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        ArrayList<String> urlList = new ArrayList<>();
        for(int i = 0; i < 10; i ++) {
            urlList.add(DogGrabber.getUrlFromJson());
        }
        return urlList;
    }

    @Override
    protected void onPostExecute(ArrayList<String> data) {
        mAdapter.updateData(data);
        super.onPostExecute(data);
        declareReady();
    }

一直在挑选这个几个小时,当我将.fit()添加到Picasso加载方法时,根本没有任何显示。

我以为我还包括我的XML文件:

主要活动布局文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/holo_blue_light">



<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/background"

    xmlns:android="http://schemas.android.com/apk/res/android">

    </android.support.v7.widget.RecyclerView>



</LinearLayout>

RecyclerView项目:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="wrap_content">
 <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible"
    android:id="@+id/image_view"
    android:scaleType="centerCrop"/>
<VideoView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:id="@+id/video_view"
    />



</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您需要设置布局管理器。与列表视图不同,Recycler视图需要布局管理器。

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);

请参阅文档here