0

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

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

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

4

1 回答 1

0

我为不久前从事的一个项目制作了一个 ArrayAdapter。基本上它所做的是检测滚动并根据您的滚动位置加载更多项目。这应该足以让你开始。仍然有 System.outs 来自测试。当然,如果您没有立即看到这是一个抽象类,因此您必须构建自己的检索方法。并且可能会根据您的需要更改此类。

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+"]";
        }
    }
}
于 2013-09-15T06:29:39.650 回答