图像未使用Fresco加载到gridView中

时间:2018-05-13 06:26:10

标签: java android xml fresco

我无法将图像从互联网加载到gridView中。我正在使用Fresco进行图像加载。当我运行应用程序时,它不会崩溃,但不会在gridView中显示图像。此外,logcat中没有显示错误。我是Android开发的新手。提前谢谢!

GalleryFragment.java

package com.example.android.tabswithswipes;

    import android.app.ActionBar;
    import android.app.Activity;
    import android.content.Context;
    import android.net.Uri;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    import android.widget.Spinner;
    import android.widget.TextView;

    import com.example.android.tabswithswipes.Utils.GridImageAdapter;
    import com.example.android.tabswithswipes.Utils.UniversalImageLoader;
    import com.facebook.drawee.view.SimpleDraweeView;
    import com.nostra13.universalimageloader.core.ImageLoader;

    import java.util.ArrayList;

    /**
     * Created by Chaitanya Shiva on 08-05-2018.
     */

    public class GalleryFragment extends Activity {

        private GridView gridView;
        private ImageView galleryImage;
        private ProgressBar mProgressBar;
        private Spinner directorySpinner;
        private Context mContext = GalleryFragment.this;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_gallery);
            galleryImage = (SimpleDraweeView)findViewById(R.id.galleryImageView);
            gridView = (GridView)findViewById(R.id.gridView);
            directorySpinner = (Spinner)findViewById(R.id.spinnerDirectory);
            mProgressBar = (ProgressBar)findViewById(R.id.progressBar);
            mProgressBar.setVisibility(View.GONE);
            ImageView closeGallery = (ImageView)findViewById(R.id.closeGallery);
            closeGallery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            TextView nextScreen = (TextView) findViewById(R.id.tvNext);
            nextScreen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            Uri uri = Uri.parse("https://www.gstatic.com/webp/gallery/4.sm.jpg");
            galleryImage.setImageURI(uri);
            tempGridSetup();
        }
        private void setUpImageGrid(ArrayList<String> imgURLs){
            GridView gridView = (GridView)findViewById(R.id.gridView);
            GridImageAdapter adapter = new GridImageAdapter(mContext, R.layout.layout_grid_imageview,"", imgURLs);
            gridView.setAdapter(adapter);
        }
        private void tempGridSetup(){
            ArrayList<String> imgURLs = new ArrayList<>();
            imgURLs.add("https://www.gstatic.com/webp/gallery/4.sm.jpg");
            imgURLs.add("https://www.gstatic.com/webp/gallery3/1.sm.png");
            imgURLs.add("https://cdn.pixabay.com/photo/2018/05/10/09/07/bleeding-heart-3387085_1280.jpg");
            imgURLs.add("https://images.idgesg.net/images/article/2017/08/android_robot_logo_by_ornecolorada_cc0_via_pixabay1904852_wide-100732483-large.jpg");
            imgURLs.add("https://www.androidcentral.com/sites/androidcentral.com/files/styles/xlarge_wm_brw/public/article_images/2018/03/android-p-logo-pixel-2-xl-5.jpg?itok=DB93lUGS");
            imgURLs.add("https://www.androidcentral.com/sites/androidcentral.com/files/styles/xlarge_wm_brw/public/article_images/2018/03/android-p-virtual-notch.jpg?itok=EnKJzDgF");
            imgURLs.add("https://www.androidcentral.com/sites/androidcentral.com/files/styles/xlarge_wm_brw/public/article_images/2018/03/samsung-galaxy-s9-plus-black-4.jpg?itok=bTitZlS_");
            imgURLs.add("https://www.extremetech.com/wp-content/uploads/2017/03/smiling-android.jpg");
            imgURLs.add("https://rimblogs.files.wordpress.com/2016/04/marshmallowman.png?w=800");
            imgURLs.add("https://www.androidcentral.com/sites/androidcentral.com/files/topic_images/2015/android-apps-topic.png");
            imgURLs.add("https://images.idgesg.net/images/article/2017/11/android-security-100741557-large.jpg");

            setUpImageGrid(imgURLs);
        }
    }

GridImageAdapter.java

package com.example.android.tabswithswipes.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.android.tabswithswipes.R;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;

/**
 * Created by Chaitanya Shiva on 12-05-2018.
 */

public class GridImageAdapter extends ArrayAdapter<String>{

    private Context mContext;
    private LayoutInflater mInflater;
    private int layoutResource;
    private String mAppend;
    private ArrayList<String> imgURLs;

    public GridImageAdapter(Context context, int layoutResource, String append, ArrayList<String> imgURLs) {
        super(context, layoutResource, imgURLs);
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mContext = context;
        this.layoutResource = layoutResource;
        this.mAppend = append;
        this.imgURLs = imgURLs;
    }

    private static class ViewHolder{
        SimpleDraweeView image;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        /*
        Viewholder build pattern (Similar to recyclerview)
         */
        final ViewHolder holder;
        if(convertView == null){
            convertView = mInflater.inflate(layoutResource, parent, false);
            holder = new ViewHolder();
            holder.image = (SimpleDraweeView) convertView.findViewById(R.id.gridImageView);

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        String imgURL = getItem(position);
        holder.image.setImageURI(imgURL);

        return convertView;
    }
}

layout_grid_imageview.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">

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridImageView"
        android:scaleType="centerCrop"/>
</RelativeLayout>

0 个答案:

没有答案