正如您在下面的Activity和Adapter类中看到的,我实现了一个无限的ListView滚动来调用API。很明显,我希望在每次滚动后显示追加到listview的最后一项,但问题是,每次添加新项时,listView会自动开始向上滚动到它的第一项。我该如何解决?
public class AdvertisementAdapter extends ArrayAdapter<Advertisement> {
private Context context;
private List<Advertisement> advertisementList;
public AdvertisementAdapter(Context context, int resource, List<Advertisement> objects) {
super(context, resource, objects);
this.context = context;
this.advertisementList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_advertisement, parent, false);
Advertisement advertisement = advertisementList.get(position);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText(advertisement.getTitle());
return view;
}
}
public class Home extends ListActivity implements OnScrollListener {
TextView output;
List<MyTask> tasks;
String uri = "http://MyHost.com/x/y/z";
int pageIndex = 1;
int pageSize = 10;
List<Advertisement> advertisementList;
AdvertisementAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
getListView().setOnScrollListener(this);
tasks = new ArrayList<>();
requestData(uri);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// empty
}
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (listView.getLastVisiblePosition() >= listView.getCount() - 1)
{
pageIndex++;
getData();
}
}
private void requestData(String uri) {
RequestPackage p = new RequestPackage();
p.setMethod("GET");
p.setUri(uri);
p.setParam("pageindex", Integer.toString(pageIndex));
p.setParam("pagesize", Integer.toString(pageSize));
MyTask task = new MyTask();
task.execute(p);
}
protected void updateDisplay() {
if (adapter != null)
{
adapter.notifyDataSetChanged();
}else
{
adapter = new AdvertisementAdapter(this,R.layout.item_advertisement, advertisementList);
}
setListAdapter(adapter);
}
private class MyTask extends
AsyncTask<RequestPackage, String, List<Advertisement>> {
@Override
protected void onPreExecute() {
tasks.add(this);
}
@Override
protected List<Advertisement> doInBackground(RequestPackage... params) {
String content = HttpManager.getData(params[0]);
if (advertisementList == null)
{
advertisementList = AdvertisementJSONParser.parseJson(content);
} else
{
advertisementList.addAll(AdvertisementJSONParser.parseJson(content));
}
return advertisementList;
}
@Override
protected void onPostExecute(List<Advertisement> result) {
tasks.remove(this);
advertisementList = result;
updateDisplay();
}
}
}
答案 0 :(得分:1)
每次必须将数据附加到adapter
数据集并致电adapter
时,您不应创建新的notifyDataSetChange
,这意味着:
if (adapter != null){
adapter.notifyDataSetChange();
}else{
adapter = new AdvertisementAdapter(this,R.layout.item_advertisement, advertisementList);
}
答案 1 :(得分:1)
updateDisplay()
protected void updateDisplay() {
if (adapter != null)
{
adapter.notifyDataSetChanged();
} else {
adapter = new AdvertisementAdapter(this,R.layout.item_advertisement, advertisementList);
setListAdapter(adapter);
}
}