检测Scroll事件并根据android中的屏幕大小查询从数据库中获取记录

时间:2013-09-15 06:01:15

标签: java android sqlite android-sqlite

我在滚动视图中有一个线性布局,我在运行时添加线性布局的视图,我在数据库中有大约1000条记录,我正在为每条记录创建一个视图,我正在运行一个循环大约1000次并在该线性布局中添加该视图(在滚动视图中),当我这样做时,这需要花费很多时间,大约30秒我认为这是因为我对每条记录进行一些计算而不是添加该视图我一次添加所有1000条记录,而不需要一次添加所有记录,

现在我认为这个问题只有在我一次添加5到6个视图时才能解决,当用户滚动时添加更多5到6个视图,......

请给我一些建议如何解决这个问题

1 个答案:

答案 0 :(得分:0)

我为一段时间工作的项目制作了一个ArrayAdapter。基本上它的作用是检测滚动并根据滚动位置加载更多项目。这应该足以让你开始。测试中仍有System.out。当然,如果您没有立即看到这是一个抽象类,所以您必须构建自己的检索方法。并且可能会改变这个课程以满足您的需求。

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.epstest.http.APIClient;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView;

public abstract class AsyncArrayAdapter<T> extends ArrayAdapter<T> implements OnScrollListener {
    private ArrayList<T> data;
    private int dataBufferSize;
    private int requestSize;

    private boolean loadingData = false;
    private MetaData metaData;

    public AsyncArrayAdapter(ListView listView, int dataBufferSize) {
        this(listView, dataBufferSize, new MetaData(0, 1));
    }

    public AsyncArrayAdapter(ListView listView, int dataBufferSize, MetaData metaData) {
        super(listView.getContext(), 0);
        data = new ArrayList<T>();

        this.dataBufferSize = dataBufferSize;
        this.metaData = metaData;
        listView.setOnScrollListener(this);
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        return position < data.size() && super.isEnabled(position);
    }

    @Override
    public T getItem(int position) {
        // TODO Auto-generated method stub
        if(position >= data.size())
            return null;
        return data.get(position);
    }

    @Override
    public int getCount() {
        return data.size() + (loadingData ? 1 : 0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position < data.size())
            return populateView(position, data.get(position), convertView, parent);
        // if this data element is outside the view then its the loading view. get and return it
        ProgressBar progBar = new ProgressBar(getContext());

        progBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        return progBar;
    }


    public void addItems(T[] items, MetaData metaData) {
        this.metaData = metaData;

        if(items.length >= dataBufferSize)
            System.out.println("Items of length [" + items.length + "] cannot be greater than the data buffer [" + dataBufferSize + "]" );

        for(int i=0;i<items.length;i++) {
            if(data.size() > metaData.offset+i)
                data.set(metaData.offset+i, items[i]);
            else
                data.add(metaData.offset+i, items[i]);
        }


        System.out.println("Added " + items.length + " items starting [" + metaData.offset + "] with max[" + metaData.max + "]"  );
        loadingData = false;
        notifyDataSetChanged();
    }

    public void requestMoreItems() {
        if(!loadingData && metaData.getMax() > data.size()) {
            loadingData = true;
            requestMoreItems(data.size(), requestSize);
            notifyDataSetChanged();
        }
    }

    protected abstract void requestMoreItems(int index, int numItems);
    protected abstract View populateView(int index, T object, View viewToPopulate, ViewGroup parent);




    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(firstVisibleItem + visibleItemCount >= totalItemCount - 1) {
            requestMoreItems();
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    public MetaData getMetaData() {
        return metaData;
    }

    public ArrayList<T> getData() {
        return data;
    }

    public void setData(ArrayList<T> data) {
        this.data = data;
    }


    public static class MetaData implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = -217672841165264339L;
        private int offset;
        private int max;

        public MetaData(int offset, int max) {
            this.offset = offset;
            this.max = max;
        }

        public int getOffset() {
            return offset;
        }

        public void setOffset(int offset) {
            this.offset = offset;
        }

        public int getMax() {
            return max;
        }

        public void setMax(int max) {
            this.max = max;
        }

        @Override
        public String toString() {
            return "MetaData[offset="+offset+", max="+max+"]";
        }
    }
}