دانلود تصاویر فقط آیتم هایی که در حال نمایش هستند، در لیست
سلام
فرض کنید یه لیست داریم که ۲۰۰تا آیتم داره و هر آیتم یه عکس داره که این عکس ها از سرور دانلود می شن. تو حالت عادی اگر کاربر اسکرول کنه و به انتهای لیست برسه تمامی ۲۰۰تا عکس در یک لحظه از سرور دانلود میشن که اینکار باعث میشه برنامه تا دانلودها تموم شه یکم کند بشه.
حالا من یه برنامه دیدم که فقط تصاویر آیتم هایی رو که تو صفحه در حال نمایش هستند رو از سرور می گرفت. مثلا اگر گوشی در یک لحظه می تونست ۱۰تا آیتم رو نشون بده، اگر کاربر سریع اسکرول میکرد و به انتهای لیست می رسید به جای اینکه تصاویر آیتم ۱ تا ۲۰۰ رو با هم دانلود کنه فقط برای آیتم های ۱۹۰ تا ۲۰۰ رو دانلود میکرد.
چه جوری میشه این کار رو کرد؟ بچه لطفا هر روشی به ذهنتون میرسه بگید که یه سر نخ دستم بیاد.
تشکر
خیر اگه تو متد fill اینکار رو بکنی باز هم همون داستانه - فرقی نداره
استک درست میکنی 10 تایی - یعنی اگه آیتم 11 اومد آیتم 1 حذف میشه
setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState == 0) Log.i("a", "scrolling stopped...");
}
});
}
وقفه اسکرول رو پیدا میکنی
استک رو میفرستی برای دانلود
آقا من ده تا تصویر آخر رو به روش زیر گرفتم:
یه فیلد توی کلاس G ساختم به اسم counter و یک آرایه هم از جنس StructImage ساختم :
public static int counter = 0;
public static StructImage[] lastItems = new StructImage[10];
بعد تو متد fill کلاس adapter این کار رو کردم
if (G.counter == 10) {
G.counter = 0;
}
G.lastItems[G.counter] = item;
G.counter++;
حالا باید چی کار کنم؟
من این یه تیکشو متوجه نمی شم. چه جوری با اون لیسنر 10تا تصویر دا خل آرایه رو بفرستم برای دانلود؟ آخه من تصاویر رو توی adapter چک می کردم بعد اگر توی دایرکتوری نبود تصویر می فرستمش برای دانلود اگر هم بود که نمایشش میدم (مثل آموزش استاد) کد متده fill کلاسAdapterImages
public void fill(ArrayAdapter<StructImage> adapter, final StructImage item, int position) {
if (G.counter == 10) {
G.counter = 0;
}
G.lastItems[G.counter] = item;
G.counter++;
imageName = HelperString.getFileName(item.url);
File thumbFile = new File(G.DIR_THUMBNAIL + "/" + imageName);
if ( !thumbFile.exists()) {
imgThumb.setImageBitmap(null);
DownloadManager.addToDownloadList(imgThumb, item.url, item.sImageId);
}
Bitmap bitmap = BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
imgThumb.setImageBitmap(bitmap);
imgThumb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (CheckInternet.isOnline()) {
Intent intent = new Intent(G.currentFragment, ActivityShare.class);
intent.putExtra("IMAGE_NAME", imageName);
G.currentFragment.startActivity(intent);
} else {
File imageFile = new File(G.DIR_IMAGES + "/" + imageName);
if ( !imageFile.exists()) {
Toast toast = new Toast(G.context);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
} else {
Intent intent = new Intent(G.currentFragment, ActivityShare.class);
intent.putExtra("IMAGE_NAME", imageName);
G.currentFragment.startActivity(intent);
}
}
}
});
}
}
بچه ها من تونستم این کا رو انجام بدم ولی گالری رو به روشی که adapter رو public می کردیم باید درست کنیم ( روشی که استاد گفت غیر استاندارده). روشم اینجوریه:
1- تو کلاس G یه آرایه 10تایی از جنس StructImage می سازیم و یک متغیر از جنس int :
public static StructImage[] lastItems = new StructImage[10];
public static int counter = 0;
متد fill کلاس AdapterImage
public void fill(ArrayAdapter<StructImage> adapter, final StructImage item, int position) {
if (G.counter == 10) {
G.counter = 0;
}
Log.i("TAG", "counter = " + G.counter);
G.lastItems[G.counter] = item;
imageName = HelperString.getFileName(G.lastItems[G.counter].url);
G.counter++;
File thumbFile = new File(G.DIR_THUMBNAIL + "/" + imageName);
imgThumb.setImageBitmap(null);
if (thumbFile.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
imgThumb.setImageBitmap(bitmap);
}
imgThumb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (CheckInternet.isOnline()) {
Intent intent = new Intent(G.currentFragment, ActivityShare.class);
intent.putExtra("IMAGE_NAME", imageName);
G.currentFragment.startActivity(intent);
} else {
File imageFile = new File(G.DIR_IMAGES + "/" + imageName);
if ( !imageFile.exists()) {
Toast toast = new Toast(G.context);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
} else {
Intent intent = new Intent(G.currentFragment, ActivityShare.class);
intent.putExtra("IMAGE_NAME", imageName);
G.currentFragment.startActivity(intent);
}
}
}
});
}
}
در FragmentNewImages که می خوام تصاویر رو دانلود کنم
public static AdapterImages adapter;
private ArrayList<StructImage> imagesAll = new ArrayList<StructImage>();
public FragmentNewImages() {}
@Override
public void onResume() {
super.onResume();
G.currentFragment = getSherlockActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new_images, container, false);
GridView gridAllImages = (GridView) view.findViewById(R.id.gridAllImages);
adapter = new AdapterImages(imagesAll);
gridAllImages.setAdapter(adapter);
gridAllImages.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
G.HANDLER.postDelayed(new Runnable() {
@Override
public void run() {
if (scrollState == 0) {
for (int i = 0; i < 10; i++) {
String imageName = HelperString.getFileName(G.lastItems[i].url);
File thumbFile = new File(G.DIR_THUMBNAIL + "/" + imageName);
if ( !thumbFile.exists()) {
DownloadManager.addToDownloadList(G.lastItems[i].url);
}
if (thumbFile.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
}
Log.i("TAG", "scrolling stopped...");
}
}
}
}, 5000);
}
@Override
public void onScroll(AbsListView view, int arg, int arg2, int scrollState) {}
});
imagesAll.clear();
getImageFromInternet();
return view;
}
private void getImageFromInternet() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//imagesAll.clear();
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("action", "all"));
String result = Webservice.readUrl("http://www.hossein-amini.ir/androidProject/webservice.php", params);
try {
JSONArray images = new JSONArray(result);
for (int i = 0; i < images.length(); i++) {
StructImage image = new StructImage();
JSONObject object = images.getJSONObject(i);
image.url = object.getString("image_name");
image.sImageId = object.getInt("image_id");
imagesAll.add(image);
}
G.HANDLER.post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
catch (JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
در روش HashMap که استاد یاد داد، با هر بار صدا کردن متده addToDownload، یه ImageView هم ارسال می کردیم خب چون متده addToDownload رو توی AdapterImage صدا می زدیم اینکار رو میشد انجام داد ولی الان که متده addToDownload رو توی FragmentNewImages صدا می زنیم چه جوری باید براش ImageView ارسال کرد؟
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .