每次在无限滚动期间添加新项目时,ListActivity中的ListView会自动向上滚动到第一个项目

时间:2014-09-09 21:15:06

标签: android android-listview android-arrayadapter android-adapter infinite-scroll

正如您在下面的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();

                }

            }

        }

2 个答案:

答案 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); 
   }
}