как загружать/обновлять изображения после прокрутки вверх и вниз в представлении сетки, работающем в фоновом режиме (асинхронная задача)

Я работаю над отображением нескольких групп изображений. Я использую представление списка для отображения каждой группы. Внутри каждой группы будет одно текстовое представление, флажок и представление сетки. В виде сетки я должен отображать каждое изображение соответствующей группы. Вот мой класс адаптера Listview:

public class CustomListAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {

    private Context listapadptercontext;
    private List<IndividualGroup> listData;
    private LayoutInflater layoutInflater;
    GridViewAdapter mAdapter;
    GridLayoutManager layoutManager;

    public CustomListAdapter(Context _c,List<IndividualGroup> _listdata){
        this.listapadptercontext = _c;
        this.listData = _listdata;
        this.layoutInflater =  (LayoutInflater) listapadptercontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.group_heading, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textView1);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.grpcheckbox);
            holder.myGridView = (MyGridView) convertView.findViewById(R.id.gridView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        IndividualGroup individualGroup = listData.get(position);
        holder.textView.setText("Set" + (position + 1));
        holder.checkBox.setOnCheckedChangeListener(this);
        layoutManager = new GridLayoutManager(listapadptercontext, 4);
        mAdapter = new GridViewAdapter(listapadptercontext,0,individualGroup.getIndividualgrpofdupes());
        holder.myGridView.setAdapter(mAdapter);
        return convertView;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        IndividualGroup individualGroup = listData.get()
    }

    static class ViewHolder {
        TextView textView;
        CheckBox checkBox;
        MyGridView myGridView;
    }
}

Мой адаптер Gridview:

public class GridViewAdapter extends ArrayAdapter
{
    private Context context;
    private int layoutResourceId;
    private LayoutInflater inflater;
    private List<String> data = new ArrayList<String>();

    public GridViewAdapter(Context context,int layoutResourceId, List<String> data) {
        super(context,layoutResourceId,data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            row = inflater.inflate(R.layout.group_duplicate_image, parent, false);
            holder = new ViewHolder(row);
            holder.image = (SquareImageView) row.findViewById(R.id.img_duplicate_image);
            holder.checkBox = (CheckBox) row.findViewById(R.id.individualcheckbox);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        String item = data.get(position);
        if (holder.image != null) {
            new GridviewLoader(context,holder.image).execute(item);
        }
        return row;
    }

    class ViewHolder implements CompoundButton.OnCheckedChangeListener {
        SquareImageView image;
        CheckBox checkBox;

        ViewHolder(View view){
//            super(view);
            image = (SquareImageView) view.findViewById(R.id.img_duplicate_image);
            checkBox = (CheckBox) view.findViewById(R.id.individualcheckbox);
            checkBox.setOnCheckedChangeListener(this);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            ImageItem imageItem = data.get(position);
        }
    }
}

Мой gridviewloader (асинхронная задача):

public class GridviewLoader extends AsyncTask<String,Void,Bitmap> {

    private final WeakReference<ImageView> imageViewReference;
    Context imageloadercontext;

    public GridviewLoader(Context c,ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
        this.imageloadercontext = c;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
//        CommonlyUsed.logmsg("%%%%%%------Filepath: "+params[0]);
        Bitmap bmp = BitmapLoader.loadBitmap(params[0], 100, 100);
        return bmp;
    }

//    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                } else {
                    Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.rsz_empty_photo);
                    imageView.setImageDrawable(placeholder);
                }

            }

        }
    }
}

Моя проблема в том, что изображения отображаются изначально. но после прокрутки вниз он исчезает. И это занимает слишком много времени, чтобы загрузить снова. Вот мои макеты:

Внутренний список.

<?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">


    <LinearLayout
        style="@style/linearLayoutStyleNoBgColor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/deepyellow">

        <TextView
            android:id="@+id/textView1"
            style="@style/textFieldStyleType1WithCheckbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|right"
            android:textColor="@color/white"
            android:textSize="15dp" />

        <CheckBox
            style="@style/checkboxStyleType1"
            android:id="@+id/grpcheckbox"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignRight="@+id/img_duplicate_image"
            android:layout_gravity="center|right"
            android:button="@null"
            android:background="@drawable/customcheckbox" />
    </LinearLayout>

<!--        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_duplicates"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"/>-->



    <com.xxx.xxx.customviews.MyGridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:clickable="true"
        android:columnWidth="100dp"
        android:drawSelectorOnTop="true"
        android:focusable="true"
        android:gravity="center"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" />



</LinearLayout>

Внутренний вид сетки:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imagelayout"
    android:weightSum="1">

    <com.remodupes.remoactivities.customviews.SquareImageView
        android:id="@+id/img_duplicate_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@null"
        android:padding="2dp"
        android:scaleType="fitXY"/>

    <CheckBox
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/individualcheckbox"
        android:layout_alignRight="@+id/img_duplicate_image"
        android:button="@null"
        android:background="@drawable/customcheckbox"
        android:layout_alignBottom="@+id/img_duplicate_image" />
</RelativeLayout>

Итак, как я могу решить эту проблему.


person Gowtham Mahadevaiah    schedule 26.03.2016    source источник
comment
загрузка, загрузка, кэширование и отображение изображений намного сложнее. Просто используйте библиотеку, которая уже реализует все для вас, например Glide или Picasso (как было предложено ответом Аджая)   -  person Budius    schedule 26.03.2016


Ответы (1)


Есть способ загрузить изображение со списком, вы можете использовать загрузчик изображений или Пикассо.

для Пикассо я предлагаю вам блог для вашего проблема вы можете использовать его, как после добавления jar picasso в свой модуль

  Picasso.
   with(State.mainContext).
   load(parseImageFile.getUrl()).
   into(null);
person Ajay Pandya    schedule 26.03.2016