Images in RecyclerView

后端 未结 3 1957
执念已碎
执念已碎 2020-12-23 12:39

I am using RecyclerView in Android and I am having some trouble with images.

I would like to display a list of the images in a particular folder with their path name

相关标签:
3条回答
  • 2020-12-23 13:31

    Yes. I think it's too big.

    You need load firstly your images(track for free memory) and after that use him.

    Not necessarily use Picasso. But you can use it.

    I recommend to you read the

    • Loading Large Bitmaps Efficiently
    • Displaying Bitmaps Efficiently
    • Caching Bitmaps

    And example :

    ItemData.java

    package com.shustikov.android;
    
    public class ItemData {
    
        private String title;
        private int imageUrl;
    
        public ItemData(String title,int imageUrl){
    
            this.title = title;
            this.imageUrl = imageUrl;
        }
    
        public String getTitle() {
            return title;
        }
    
        public int getImageUrl() {
           return imageUrl;
        }
    }
    

    RecyclerViewExampleAdapter.java

    package com.shustikov.android;
    
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class RecyclerViewExampleAdapter extends RecyclerView.Adapter<RecyclerViewExampleAdapter.ViewHolder> {
        private ItemData[] itemsData;
    
        public RecyclerViewExampleAdapter(ItemData[] itemsData) {
            this.itemsData = itemsData;
        }
    
        @Override
        public RecyclerViewExampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            View itemLayoutView = LayoutInflater.from(parent.getContext())
                                   .inflate(R.layout.item_layout, null);
    
            ViewHolder viewHolder = new ViewHolder(itemLayoutView);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
    
            viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
            viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
         }
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
    
            public TextView txtViewTitle;
            public ImageView imgViewIcon;
    
            public ViewHolder(View itemLayoutView) {
                super(itemLayoutView);
                txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
                imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
            }
        }
    
    
        @Override
        public int getItemCount() {
            return itemsData.length;
        }
    }
    
    0 讨论(0)
  • 2020-12-23 13:37

    You're loading your images on UI thread, that's the problem. Loading file from disk or network takes some time. You can achieve the same, but using Picasso library. Please refer here.

    0 讨论(0)
  • 2020-12-23 13:39

    It takes time to mold the code using caching method and loading images before the fragment is displayed. An easy approach would be to use the Picasso Library. I myself used Picasso and now images load quite fast as I scroll.

    Here is my code:

    public void bindPhoto(Photo photo) {
        mPhoto = photo;
        mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto);
        Uri uri=Uri.fromFile(mPhotoFile);
    
        if(mPhotoFile==null || !mPhotoFile.exists()){
            int imgdrawable=R.drawable.ic_action_name3;
            mThumbnail.setImageDrawable(getResources().getDrawable(imgdrawable));
        } else {
            Picasso.with(getActivity()).load(uri).fit().into(mThumbnail);
        }
    
    
        mTitleTextView.setText(mPhoto.getTitle());
        String dateFormat = "EEE, MMM dd";
        dateString = DateFormat.format(dateFormat, mPhoto.getDate()).toString();
        mDateTextView.setText(dateString);
    }
    
    0 讨论(0)
提交回复
热议问题