使用NEXT和PREVIOUS按钮进行ListView分页

时间:2014-05-28 11:01:06

标签: android

我有一个listview,使用webservice加载200个项目。所有200个项目都显示在单个列表视图中。

我想首先加载 1-20 项目,当我点击 NEXT 按钮时,我需要加载 21-40 项目等等上。反之,当我点击 PREVIOUS 按钮时,我需要加载 1-20 项目。

如何为此实现分页?

我试过了:

       static ArrayList<String> Code = new ArrayList<String>();  
       //Code is ArrayList where i get 200 items from Service    

       int rowSize = 20;
        // rowSize is number of items i want per page 

       static ArrayList<String> TempCode = new ArrayList<String>();
       //TempCode is temporary ArrayList where iam trying to store items per page


       Button NextButton;

       NextButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               // TODO Auto-generated method stub



        int size = Code.size() / rowSize;
          for (int j = 0; j < size; j++) {
                final int k;
                k = j;
          addItem(k);

        }

        }

        }); 


     public void addItem(int count) {


     TempCode.clear();

     count = count * rowSize;


     /**
      * fill temp array list to set on page change
      */

     for (int j = 0; j < rowSize; j++) {

         TempCode.add(j, Code.get(count));

         count = count + 1;


     }


     Adapter adapter=new Adapter(getApplicationContext(),TempCode);

     listView.setAdapter(adapter);

    }

请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:0)

    int _currentPage = 1;
    public int FeedCountPerPage = 20;

//Call the below code in one function on click of your next button 
//And do the reverse on click of previous button
    _currentPage += 1;
                try {
                    int _feedCount = _currentPage * FeedCountPerPage;
                    if (_feedCount <= arrPhotoData.size()) {
                        tmpList =  arrPhotoData.subList(_feedCount - FeedCountPerPage, _feedCount);
                        adderList.addAll(arrPhotoData.subList(0, FeedCountPerPage));
                        adderList.addAll(tmpList);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    if (adderList == null || adderList.size() == 0)
                    adapter = new InstagramListAdapter(_myActivity, arrPhotoData);
                else
                    adapter = new InstagramListAdapter(_myActivity, adderList);

I have posted this answer as a context of my project. I hope you'll have some understanding from this.