如何从JSON加载多个图像并在android中的画廊视图中显示?

时间:2016-02-15 10:55:51

标签: android android-layout android-volley android-image android-gallery

我正在使用Gallery选项开发应用程序。我在JSON中有一些网址。我已经解析它并保存在字符串ArrayList中。现在我只想在GalleryView中显示这些图像。我已经浏览了各种参考资料,但没有帮助。我也使用了UniversalImageLoader,它仅适用于一个网址。我需要加载我ArrayList中存在的多个网址。请给我一些实现它的提示。

已更新

这是我的Gallery Activity类

public class Gallery extends AppCompatActivity {
public ArrayList<GalleryItem> imageURL;
private ImageView selectedImageView;
ArrayList<Drawable> dataDraw= new ArrayList<>();
private GridView mGridView;
public Gallery gallery;

private GalleryImageAdapter galImageAdapter;

public String url="my_url";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
    mGridView = (GridView) findViewById(R.id.galleryGrid);
    imageURL=new ArrayList<>();



    new AsyncHttpTaskforGallery().execute(url);
//        galImageAdapter = new GalleryImageAdapter(this, dataDraw);
    galImageAdapter=new GalleryImageAdapter(this,R.layout.gallery_item,imageURL);
    mGridView.setAdapter(galImageAdapter);


}



public class AsyncHttpTaskforGallery extends AsyncTask<String, Void, Integer> {

    @Override

    protected Integer doInBackground(String... params) {

        Integer result = 0;

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
            int statusCode = httpResponse.getStatusLine().getStatusCode();


            if (statusCode == 200) {

                String response = streamToString(httpResponse.getEntity().getContent());
                parseResult(response);
                result = 1;
            }
            else {
                result = 0;
            }
        } catch (Exception e) {

            Log.d("TAG", e.getLocalizedMessage());
        }

        return result;
    }

    @Override

    protected void onPostExecute(Integer result) {


        if (result == 1) {
            galImageAdapter.setGridData(imageURL);
        }

        else {

        }


    }
}


String streamToString(InputStream stream) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
    String line;
    String result = "";

    while ((line = bufferedReader.readLine()) != null) {

        result += line;
    }


    if (null != stream) {

        stream.close();
    }

    return result;
}


private void parseResult(String result) {

    try {

        JSONArray posts = new JSONArray(result);

        GalleryItem galleryItem;
        for (int i = 0; i < posts.length(); i++) {

            JSONObject post = posts.optJSONObject(i);
            String path = post.optString("path");
            String newText=path.replace("\\", "/");
            String image="http://***/"+newText;
            Log.e("imageURL", image);
            galleryItem=new GalleryItem();
            galleryItem.setImage(image);
            imageURL.add(galleryItem);

        }

    }

    catch (JSONException e) {

        e.printStackTrace();

    }
}
  }

GalleryAdapter

public class GalleryImageAdapter extends ArrayAdapter<GalleryItem> {

private Context mContext;
private int layoutResourceId;
private ArrayList<GalleryItem> mGalleryData = new ArrayList<GalleryItem>();
public GalleryImageAdapter(Context mContext, int layoutResourceId, ArrayList<GalleryItem> mGalleryData) {
    super(mContext, layoutResourceId, mGalleryData);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.mGalleryData = mGalleryData;
}
public void setGridData(ArrayList<GalleryItem> mGalleryData) {
    this.mGalleryData = mGalleryData;
    notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;

    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();

        holder.imageView = (ImageView) row.findViewById(R.id.IMVgallery);
        row.setTag(holder);
    } else {
         holder = (ViewHolder) row.getTag();
    }

    GalleryItem item = mGalleryData.get(position);

    Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
    return row;
}
static class ViewHolder {

    ImageView imageView;
}

}

在gridview中显示图像效果不佳。我需要在galleryview中显示,在点击图片时,它应该展开。

2 个答案:

答案 0 :(得分:2)

GalleryView - &gt;使用带有GridLayoutManager的RecyclerView

然后在RecyclerView Adapter的ViewHolder初始化中,使用Picasso库将图像加载到相应的ImageView中。

现在当用户点击单个图片时会发生什么?您打算以全屏视图打开它吗?

如果是,请使用ViewPager。再次使用类似的技术。使用Picasso在ViewPager适配器的相应ImageView中加载图像。

对于毕加索图书馆,请参阅: http://square.github.io/picasso/

如果您需要更多帮助,请与我们联系。

<强>更新

  1. 尝试了解如何使用GridLayoutManager实现RecyclerView

  2. 您的recyclerview项目视图中应包含一个imageview,您将在ViewHolder的初始化中使用Picasoo库填充图像。

  3. 在onView of ImageView中 - &gt;使用相同的URI集打开另一个活动,并使用这些URI使用这些图像填​​充ViewPager。

  4. 更新2

    可以说,您的图库适配器是“MyAdapter”。它应该是这样的:

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
        private Context mContext;
        private ArrayList<String> mUrls;
    
        public MyAdapter(Context context, ArrayList<String> urls) {
            mContext = context;
            mUrls = urls;
        }
    
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_gallery_item_layout, null);
            MyViewHolder rcv = new MyViewHolder(layoutView);
            return rcv;
        }
    
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            Picasso.with(mContext).load(mUrls.get(position)).into(holder.mImageView);
            //Do something here
        }
    
        @Override
        public int getItemCount() {
            return mUrls.size();
        }
    
        public class MyViewHolder extends RecyclerView.ViewHolder{
    
            private final ImageView mImageView;
            //Other views
    
            public MyViewHolder(final View itemView) {
                super(itemView);
                mImageView = itemView.findViewById(R.id.gallery_item_imageview);
                mImageView.setOnClickListener(new View.OnClickListener(){
    
                    @Override
                    public void onClick(View view){
                        //This is were the magic happens:
                        //We are opening the full image viewer activity that contains a ViewPager to show the images
                        Intent intent = new Intent(mContext, MyFullViewActivity.class);
                        intent.putStringArrayListExtra("urls", mUrls);
                        intent.putInt("starting_index", getPosition());
                        mContext.startActivity(intent);
                    }
    
                });
                //Initialize other views here
    
            }
        }
    }
    

    请注意,在ImageView的onCLick中,我们将启动另一个活动“MyFullViewActivity”,此活动在项目布局中有一个带有imageview的ViewPager。我们还将两个意图附加内容传递给此活动:

    1. “urls”:这是ViewPager将使用的网址列表

    2. “starting_index”:它会告诉活动从此值中提供的索引开始。即如果用户在您的图库中的第二个图像上进行了clciks,则MyFullViewActivity应该以该索引开头,而不是每次都以第一个索引开头。

    3. 现在您可以实现ViewPagerAdapter并相应地更改图像(使用Picasso,如上例所述)。

      如果您需要更多帮助,请与我联系。

      更新3:

      以下是初始化recyclerview的方法:

      RecyclerView mGalleryViewRecyclerView = findViewById(R.id.my_gallery_recycler_view);
      GridLayoutManager mGridLayoutManager = new GridLayoutManager(mContext, 2); // here 2 indicates the number of columns in each row.
      mGalleryViewRecyclerView.setLayoutManager(mGridLayoutManager);
      

答案 1 :(得分:0)

您可以使用picasso和适配器执行此操作: 网格适配器:

var opacity = 0.7;
$(".current").css("cssText", $('.current').css('cssText') + "opacity: "+ opacity +" !important;");

在list_item.xml中定义图像视图属性,在主活动中将此适配器设置为网格视图并传递网址列表。