在backgroung中运行的网格视图中向上和向下滚动后如何加载/更新图像(异步任务)

时间:2016-03-26 07:13:44

标签: android listview gridview android-asynctask

我正在努力展示多组图片。我使用列表视图显示每个组。每个组内部都有一个文本视图,复选框和网格视图。 在网格视图中,我必须显示相应组的每个图像。 这是我的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>

那么我该如何解决这个问题呢。

1 个答案:

答案 0 :(得分:1)

有一种方法可以使用列表加载图像,您可以使用图像加载器或毕加索

对毕加索我建议你blog for your problem 你可以像将毕加索的jar添加到你的模块中一样使用它

  Picasso.
   with(State.mainContext).
   load(parseImageFile.getUrl()).
   into(null);