4

我的列表视图中有从 Internet 下载的图像。我想在我的 getView() 中放一些东西,这样如果用户快速滚动/滚动,它就不会下载图像?

我怎样才能开始这样做?

4

1 回答 1

2

您可以按照以下步骤延迟加载 ListView 中的视图。

首先,你应该让你的ListView对象和YOUR_COSTOM_ADAPTER_OBJECT类变量。然后,为您的 ListView 设置一个 onScroll 侦听器

从 ListView 的 onScroll 侦听器获取
滚动速度:从 onScroll 侦听器获取滚动速度,当速度变快时,告诉适配器不要加载图像。

示例代码:

class YourClass extends Activity
{
YOUR_CUSTOM_LISTVIEW_ADAPTER adapter;
ListView YOUR_LISTVIEW;
ArrayList<YOUR_DATA_TYPE> listViewData;

onCreate(...){
.....
.....
//ListView data Initialization
listViewData = new ArrayList<YOUR_DATA_TYPE>();
listViewData.clear();
YOUR_LISTVIEW = (ListView)findViewById(R.id.YOUR_LISTVIEW_ID);
//adapter Initialization
adapter = new YOUR_CUSTOM_LISTVIEW_ADAPTER(....);
YOUR_LISTVIEW.setAdapter(adapter);
}

private OnScrollListener onScrollListener = new OnScrollListener() {

private int previousFirstVisibleItem = 0;
private long previousEventTime = 0;
private double speed = 0;

@Override
public void onScroll(HtcAbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
{
    if (previousFirstVisibleItem != firstVisibleItem)
    {
        long currTime = System.currentTimeMillis();
        long timeToScrollOneElement = currTime - previousEventTime;
        speed = ((double)1/timeToScrollOneElement)*1000;

        previousFirstVisibleItem = firstVisibleItem;
        previousEventTime = currTime;

        Log.d("DBG", "Speed: " +speed + " elements/second");

        //Tell adapter to not load images if it has reached a specific speed
        if(speed>YOUR_DESIRED_VALUE)
        {
            doNotLoadImagesInListView();
        }
        else
        {
            loadImagesInListView();
        }
    }
}

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

void doNotLoadImagesInListView()
{
    adapter.isScrollFast = true;
    adapter.notifyDataSetChanged();
    YOUR_LISTVIEW.setAdapter(adapter);
}

void loadImagesInListView()
{
    adapter.isScrollFast = false;
    adapter.notifyDataSetChanged();
    YOUR_LISTVIEW.setAdapter(adapter);
}


}


配置您的自定义列表适配器以进行相应的更改:

public class yourCustomAdapter extends BaseAdapter
{
    public Boolean isScrollFast;

public yourCustomAdapter(..... , ....., Boolean isScrollFast)
{
    ....
    this.isScrollFast = isScrollFast;
    .....
}

@Override
public int getCount() 
{
    .....
}

@Override
public Object getItem(int position) 
{
    .....
}

@Override
public long getItemId(int position) 
{
    .....
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    .......
    if(isScrollFast==true)
    {
        //Don't Load Images
    }
    else
    {
        //Load Images normally
    }

    return vi;
}
}

我希望这对你有用。

于 2013-08-08T18:06:06.310 回答