在ListView中自动加载更多项(XML解析)

时间:2014-02-22 15:56:24

标签: android listview android-listview android-adapter

我已经对ListView进行了XML解析,现在我需要实现load-more功能。当用户滚动到底部以及如何限制ListView中的项目数时,我知道如何指示。但是,当用户滚动到列表末尾时,有什么方法可以显示更多项目吗?

这是我的适配器类:

public class ClubsAdapter extends ArrayAdapter<LeagueClub> {

    ImageLoader imageLoader;
    DisplayImageOptions options;
    Context mContext;
    public static List<LeagueClub> mClubs;

    @SuppressWarnings("deprecation")
    public ClubsAdapter(Context mContext, int textViewResourceId, List<LeagueClub> clubs) {
        super(mContext, textViewResourceId, clubs);



        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext).build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);


        options = new DisplayImageOptions.Builder()
        .cacheInMemory()
        .cacheOnDisc()

        .build();

        mClubs=clubs;
    }



    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        RelativeLayout row = (RelativeLayout)convertView;
        Log.i("PremierLeague", "getView pos = " + pos);
        if(null == row){

            LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = (RelativeLayout)inflater.inflate(R.layout.row_site, null);
        }

        final ImageView clubLogo = (ImageView)row.findViewById(R.id.clubLogo);
        TextView nameTxt = (TextView)row.findViewById(R.id.nameTxt);
        TextView aboutTxt = (TextView)row.findViewById(R.id.aboutTxt);
        TextView stadiumTxt = (TextView)row.findViewById(R.id.stadiumTxt);
        final ProgressBar indicator = (ProgressBar)row.findViewById(R.id.progress);

        indicator.setVisibility(View.VISIBLE);
        clubLogo.setVisibility(View.INVISIBLE);

        ImageLoadingListener listener = new ImageLoadingListener(){



            @Override
            public void onLoadingStarted(String arg0, View arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLoadingCancelled(String arg0, View arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
                indicator.setVisibility(View.INVISIBLE);
                clubLogo.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String arg0, View view, FailReason arg2) {
                indicator.setVisibility(View.INVISIBLE);
                ImageView imageView = (ImageView) view.findViewById(R.id.clubLogo);
                imageView.setVisibility(View.VISIBLE);

            }

        };

        imageLoader.displayImage(getItem(pos).getLogo(), clubLogo,options, listener);

        nameTxt.setText(getItem(pos).getName());
        aboutTxt.setText(getItem(pos).getAbout());
        stadiumTxt.setText(getItem(pos).getStadium());




        return row;


    }

    public int getCount() {
        return mClubs.size(); //If i set return 10; it limits items to max 10
}


}

这是我的片段:

public class Fragment2 extends Fragment {
    static PullToRefreshListView mListView;
    static ClubsAdapter mAdapter;
    Context mContext;
    ProgressBar mProgress;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_2, container, false);

        mListView = (PullToRefreshListView) rootView.findViewById(R.id.pull_to_refresh_listview1);
        mProgress = (ProgressBar) rootView.findViewById(R.id.loading_clubs);
        mProgress.setVisibility(View.VISIBLE);
        ClubsDownloadTask task = new ClubsDownloadTask();
        task.execute();

        mListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // Do work to refresh the list here.
                mProgress.setVisibility(View.INVISIBLE);
                ClubsDownloadTask task = new ClubsDownloadTask();
                task.execute();
            }
        });
        return rootView;

        }


    private class ClubsDownloadTask extends AsyncTask<Void, Void, Void>{
        private ClubsAdapter mAdapter; 



        @Override
        protected Void doInBackground(Void... arg0) {




            try {
                Downloader.DownloadFromUrl("http://dl.dropboxusercontent.com/s/h2qc41k2yy3c1ir/clubs.xml", getActivity().openFileOutput("clubs.xml", Context.MODE_PRIVATE));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }


            return null;

        }

        protected void onPreExecute() {



        }

        protected void onPostExecute(Void result){
            mListView.onRefreshComplete();
            mProgress.setVisibility(View.INVISIBLE);
            mAdapter = new ClubsAdapter(getActivity(), -1 , ClubsXmlPullParser.getItemsFromFile(getActivity()));
            mListView.setAdapter(mAdapter);
            if(ClubsAdapter.mClubs != null){

                mAdapter.clear();
            for (int i = 0; i < ClubsAdapter.mClubs.size(); i++) {

                      mAdapter.add(ClubsAdapter.mClubs.get(i));

                      }  
            mListView.setOnScrollListener(new OnScrollListener() {


                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                        int totalItemCount) {
                    if(mListView.getRefreshableView().getCount()!=0&&mListView.getRefreshableView().getCount()>0&&mAdapter.getCount()!=0){
                    if (mListView.getRefreshableView().getLastVisiblePosition() == mListView.getRefreshableView().getAdapter().getCount() - 1
                            && mListView.getRefreshableView()
                            .getChildAt(mListView.getRefreshableView().getChildCount() - 1)
                            .getBottom() <= mListView.getRefreshableView().getHeight()) {

                        //Here do I need to load more items

                    }
                }

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


                    if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
                        View currentFocus = getActivity().getCurrentFocus();
                        if(currentFocus != null) {
                            currentFocus.clearFocus();
                        }
                    }



                }

            });



        }

    }
    }
    }

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

您可以使用此适配器https://github.com/commonsguy/cwac-endless 使用此适配器,当用户滚动到列表末尾时,它将调用一个可以实现的功能来加载更多项目。