我试图从listview适配器的开头删除少量元素,当元素数量很大时,为了提高内存效率,我想保持相同的可见元素,但这不会因为删除而发生 以下是相应的代码
listView.setOnScrollListener(new OnScrollListener()
{
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
synchronized(loadLock)
{
if(totalItemCount>0)
{
if(totalItemCount-firstVisibleItem-visibleItemCount<10 && offset!=highOffset &&load)
{
load=false;
offset=highOffset;
loadEvents();
}
}
}
}
});
}
public void loadEvents()
{
Response.Listener<JSONObject> response = new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Log.d(TAG, response.toString());
JSONArray events = null;
try
{
events = response.getJSONArray("events");
}
catch (JSONException e1)
{
e1.printStackTrace();
}
if (events != null)
{
Event event;
for (int i = 0; i < events.length(); i++)
{
//filling events list
}
highOffset+=events.length();
adapter.notifyDataSetChanged();
cleanListView();
load=true;
}
}
};
CustomRequest eventReq = new CustomRequest(Method.POST, URL, params,
response, errorListener);
AppController.getInstance().addToRequestQueue(eventReq);
}
public void cleanListView()
{
if(eventsList.size()>=1000)
{
for(int i=0;i<100;i++)
{
eventsList.remove(0);
adapter.notifyDataSetChanged();
}
lowOffset+=100;
}
}
答案 0 :(得分:0)
我建议使用RecyclerView而不是ListView,你将在android中自动优化内存性能。
答案 1 :(得分:0)
我会尝试类似setSelectionFromTop的内容,如果我知道有多少项被删除等等