如何通过上下滚动触发Glide下载滚动视图中的图片

How to trigger Glide to download images in scroll view by scrolling up and down

所以,我有一个加载 sql 查询的 AsyncTask,结果将显示在 table 布局中。 "extractArrayList" 方法将查询结果提取到数组列表中并 "populateItemsTable" 方法填充 table 以显示结果。

public boolean extractArrayList(ArrayList<JSONObject> result_obj) {
    try {

        for (int i = 0; i < result_obj.size(); i++) {
            item_createDate.add(result_obj.get(i).getString("CreateDate"));
            item_name.add(result_obj.get(i).getString("Name"));
            item_unit.add(result_obj.get(i).getString("Unit"));
            item_textLable.add(result_obj.get(i).getString("TextLable"));
            item_topSale.add(result_obj.get(i).getString("TopSale"));
            item_Fee.add(result_obj.get(i).getString("Fee"));
            item_moreInfo.add(result_obj.get(i).getString("MoreInfo"));
            item_IfImageUploaded.add(result_obj.get(i).getString("IfImageUploaded"));
        }
        Collections.reverse(item_createDate);
        Collections.reverse(item_name);
        Collections.reverse(item_textLable);
        Collections.reverse(item_unit);
        Collections.reverse(item_Fee);
        Collections.reverse(item_IfImageUploaded);
        Collections.reverse(item_moreInfo);
        Collections.reverse(item_topSale);
        return true;
    } catch (JSONException e) {
        e.printStackTrace();
        return false;
    }
}

public boolean populateItemsTable(){
    TableLayout items_table = (TableLayout) findViewById(R.id.items_table);
    for (int i = 0; i < item_createDate.size(); i++) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View tr = layoutInflater.inflate(R.layout.row_layout, null, false);

        tr.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });

        tr.setOnGenericMotionListener(new View.OnGenericMotionListener() {
            @Override
            public boolean onGenericMotion(View v, MotionEvent event) {

                return false;
            }
        });

        tr.setOnHoverListener(new View.OnHoverListener() {
            @Override
            public boolean onHover(View v, MotionEvent event) {

                return false;
            }
        });

        tr.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {

            }
        });
        TextView item_nameTxt = (TextView) tr.findViewById(R.id.item_nameTxt);
        TextView item_feeTxt = (TextView) tr.findViewById(R.id.item_feeTxt);
        TextView item_textTxt = (TextView) tr.findViewById(R.id.item_textTxt);
        ImageView item_imgView = (ImageView) tr.findViewById(R.id.item_imgView);
        final Button add_basket_btn = (Button) tr.findViewById(R.id.basket_btn);
        final Button more_info_btn = (Button) tr.findViewById(R.id.moreInfo_btn);

        item_nameTxt.setText(item_name.get(i).toString());

        Float item_fee_float = Float.parseFloat(item_Fee.get(i));
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
        DecimalFormat df = (DecimalFormat) nf;
        item_feeTxt.setText("قیمت: " + df.format(item_fee_float) + " ریال / " + item_unit.get(i).toString());
        item_textTxt.setText(item_textLable.get(i).toString());

        if (item_IfImageUploaded.get(i).equals("Yes")) {
            String pic_url = item_createDate.get(i).toLowerCase().replaceAll("[-]", "");
            pic_url = item_createDate.get(i).toLowerCase().replaceAll("[:]", "");
            pic_url = pic_url.toLowerCase().replaceAll("[ ]", "");
            pic_url = pic_url.toLowerCase().replaceAll("[-]", "");
            pic_url = "http://www.coffeetaxi.ir/iteminfo/photo/" + pic_url + ".jpg";

            Glide.with(this)
                    .load(pic_url.toString())
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            spinner.setVisibility(View.GONE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            spinner.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .into(item_imgView);
        } else {
            item_imgView.setImageResource(R.mipmap.no_image);
        }

        add_basket_btn.setTag(i);
        add_basket_btn.setBackgroundResource(R.mipmap.basket_btn);
        add_basket_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Integer code;
                code = Integer.parseInt(add_basket_btn.getTag().toString());
                askForQTY(itemsActivity.this, code, item_name.get(code), "مقدار/تعداد مورد نظر را وارد نمایید", false);
            }
        });

        more_info_btn.setTag(i);
        more_info_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                moreInfoCall = Integer.parseInt(more_info_btn.getTag().toString());
                itemsActivity.this.startActivity(new Intent(itemsActivity.this, MoreinfoActivity.class));
            }
        });

        if (item_moreInfo.get(i).equals("none")) {

            more_info_btn.setVisibility(View.INVISIBLE);

        } else {

        }
        items_table.addView(tr);
    }

    return true;
}

AsyncTask的onPostExecute方法分别调用了以上两个方法。 此代码工作正常,但一个: 结果的所有图像将立即下载,这会导致高内存分配。 我想要的是对上面的代码进行更改(可以进行大量更改;))以在滚动到特定项目时使 Glide 加载图像。(类似于 Instagram 应用程序所做的事情!) 这是主要的 activity XML:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="0dp"
    android:fillViewport="true"
    android:scrollbars="vertical" >
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/items_table"
        android:scrollIndicators="left"
        android:fillViewport="true"
        android:showDividers="middle"></TableLayout>
</ScrollView>

和 table 行布局,以防:

<RelativeLayout
    android:id="@+id/img_rel"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:gravity="left"
    android:layout_weight="1">

<ImageView
    android:adjustViewBounds="true"
    android:maxHeight="160dp"
    android:maxWidth="160dp"
    android:id="@+id/item_imgView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:scaleType="fitStart"/>
</RelativeLayout>

<RelativeLayout
    android:layout_marginTop="10dp"
    android:id="@+id/desc_rel"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginRight="0dp"
    android:gravity="right"
    android:layout_weight="1">

    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/item_nameTxt"
        android:inputType="textMultiLine"
        android:scrollHorizontally="false"
        android:textSize="16dp"
        android:layout_alignRight="@+id/basket_btn"
        android:textAlignment="gravity"
        android:layout_gravity="right"
        android:gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:inputType="textMultiLine"
        android:scrollHorizontally="false"
        android:id="@+id/item_feeTxt"
        android:layout_alignRight="@+id/basket_btn"
        android:textAlignment="gravity"
        android:layout_gravity="right"
        android:gravity="right"
        android:layout_width="wrap_content"
        android:layout_below="@+id/item_nameTxt"
        android:layout_height="wrap_content" />
    <TextView
        android:inputType="textMultiLine"
        android:scrollHorizontally="false"
        android:id="@+id/item_textTxt"
        android:layout_alignRight="@+id/basket_btn"
        android:textAlignment="gravity"
        android:layout_gravity="right"
        android:gravity="right"
        android:layout_below="@+id/item_feeTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/basket_btn"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"
        android:background="@mipmap/basket_btn"
        android:layout_width="40dp"
        android:layout_below="@+id/item_textTxt"
        android:layout_height="40dp" />
    <Button
        android:id="@+id/moreInfo_btn"
        android:layout_marginRight="5dp"
        android:background="@mipmap/more_info"
        android:layout_below="@+id/item_textTxt"
        android:layout_toLeftOf="@+id/basket_btn"
        android:layout_width="40dp"
        android:layout_height="40dp" />

</RelativeLayout>

如有任何提示,我们将不胜感激。

你要求Glide在布局膨胀时立即下载图片,但是不行。

在我看来,您应该使用 ListView 或 RecycleView,而不是 TableLayout。 Glide 应该只在绑定视图持有者时下载图像。