当用户向下滚动到列表末尾时,如何设置在Cwac-Endlessadapter中加载列表项的限制

时间:2013-04-19 10:31:08

标签: android listview endlessadapter

例如: - 如果我在列表中加载前5个项目,则向下滚动下载5个项目

并且因为它是一个无尽的适应者,这个程序必须无休止地重复 这就是我试过的方法 - > foliowing是我的demoadapter代码: -

   public class MyDemoAdapter extends EndlessAdapter {

private static int cutting_int =5;
public static int batch_repeat = 0;
public int check_batch = 0;
private Context mcontxt;
private RotateAnimation rotate = null;
ArrayList<DownloadOffersClass> tempList = new ArrayList<DownloadOffersClass>();
private static int mLastOffset = 0;
private ArrayList<DownloadOffersClass> list = new ArrayList<DownloadOffersClass>();
private int LIST_SIZE;


public MyDemoAdapter(Context context, int textViewResourceId,
        ArrayList<DownloadOffersClass> list, int lIST_SIZE) {
    super(new DownloadedOffersAdapter(context,
            R.layout.offer_listview_item, list));
    this.mcontxt = context;
    this.list = list;
    this.LIST_SIZE = lIST_SIZE;
    rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
            0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(600);
    rotate.setRepeatMode(Animation.RESTART);
    rotate.setRepeatCount(Animation.INFINITE);
}

@Override
protected View getPendingView(ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mcontxt
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View row = inflater.inflate(R.layout.row, null);

    View child = row.findViewById(android.R.id.text1);

    child.setVisibility(View.GONE);

    child = row.findViewById(R.id.throbber);
    child.setVisibility(View.VISIBLE);
    child.startAnimation(rotate);

    return (row);
}

@Override
protected boolean cacheInBackground() {
    SystemClock.sleep(2000);

    if (check_batch != batch_repeat) {
        tempList.clear();
        int lastOffset = getLastOffset();
        if (lastOffset < LIST_SIZE) {
            int limit = lastOffset + cutting_int;
            for (int i = (lastOffset + 1); (i <= limit && i < LIST_SIZE); i++) {
                tempList.add(list.get(i));
            }
            setLastOffset(limit);

            if (limit < 50) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

@Override
protected void appendCachedData() {
    if (getWrappedAdapter().getCount() < 50) {
        @SuppressWarnings("unchecked")
        DownloadedOffersAdapter a = (DownloadedOffersAdapter) getWrappedAdapter();

        check_batch = check_batch + 1;
        Log.v("Check", " " + check_batch);
        for (int i = cutting_int; i < cutting_int + cutting_int; i++) {
            a.add(list.get(i));
        }
    }
}

public static void setLastOffset(int i) {
    mLastOffset = i; 
}

private int getLastOffset() {
    return mLastOffset;
}

}

3 个答案:

答案 0 :(得分:3)

修改cacheInBackground()

if (check_batch != batch_repeat) {
            tempList.clear();
            int lastOffset = getLastOffset();
            if (lastOffset < LIST_SIZE) {
                int limit = lastOffset + cutting_int;
                for (int i = (lastOffset + 1); (i <= limit && i <                                           LIST_SIZE); i++) {
                    tempList.add(list.get(i));
                }
                setLastOffset(limit);

                if (limit <= 50) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            if (!firstTry) {// use this boolean to do it only once
                tempList.clear();
                int notDivisible = LIST_SIZE % cutting_int;
                if (!(notDivisible == 0)) {
                    firstTry = true;
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }

用这个替换你的appendCachedData()方法:

  protected void appendCachedData() {

        DownloadedOffersAdapter a = (DownloadedOffersAdapter) getWrappedAdapter();
        if (check_batch != batch_repeat) {
            if (getWrappedAdapter().getCount() <= 50) {
                check_batch = check_batch + 1;
                Log.v("Check", "append chk_batch " + check_batch);
                for (int i = cutting_int; i < cutting_int + cutting_int; i++) {
                    a.add(list.get(i));
                }
            }
        } else {// Append extra entries to list i.e less than cutting_int
            int notDivisible = LIST_SIZE % cutting_int;
            for (int i = (LIST_SIZE - notDivisible); i < LIST_SIZE - 1; i++) {
                a.add(list.get(i));
            }
            return;
        }
    }

答案 1 :(得分:0)

您将在listadapter上为ImageLoader创建对象。

ImageLoader lazyload = new ImageLoader(context);

public ImageLoader(Context context) {
        FileCache fileCache = new FileCache(context);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
    }

然后将图像设置为适配器getView()。

  ImageView image = (ImageView) view.findViewById(R.id.yourImage);

    lazyload.displayImage(image);

加载图片

public void displayImage(String url, ImageView imageView) {
    if (bitmap != null)
        imageView.setImageBitmap(bitmap);

答案 2 :(得分:0)

  

例如: - 如果我在列表中加载前5个项目,则向下滚动下载5个项目

您有责任使用足够的项目填充适配器以保证滚动,并且5不太可能。

当您被告知要加载其他数据时,您有责任确定要加载的其他项目数。

  

因为它是一个无尽的适应者,这个程序必须无休止地重复

或直到您告诉EndlessAdapter没有更多数据,例如从false返回cacheInBackground()。这里的想法是,当您进行Web服务调用以获取下一组数据时,您还应确定是否已到达可用数据的末尾。