代码之家  ›  专栏  ›  技术社区  ›  Bhavesh Jethani

pull以刷新和加载更多列表视图,如facebook[已关闭]

  •  5
  • Bhavesh Jethani  · 技术社区  · 13 年前

    这里我用的是滑动抽屉。在点击时 主页图标 它显示3个选项卡
    1) 我应该申请哪一个概念的标签?
    2) 我想申请 pulltoreferesh loadmore 像脸书一样在列表视图中? 在这一点上,您还看到了当向上滚动进度条时,它会被隐藏,请求会被取消。

    enter image description here

    3 回复  |  直到 11 年前
        1
  •  4
  •   Bhavesh Jethani    12 年前
    public class ListDemo extends Fragment{
        ArrayAdapter<String> files;
        private LinkedList<String> mListItems;
        PullAndLoadListView lyt ;
        //  ListView lv1;
    
        // The data to be displayed in the ListView
        private String[] mNames = { "Fabian", "Carlos", "Alex", "Andrea", "Karla",
                "Freddy", "Lazaro", "Hector", "Carolina", "Edwin", "Jhon",
                "Edelmira", "Andres" };
    
        // The data to be displayed in the ListView
        private String[] mAnimals = { "Perro", "Gato", "Oveja", "Elefante", "Pez",
                "Nicuro", "Bocachico", "Chucha", "Curie", "Raton", "Aguila",
                "Leon", "Jirafa" };
    
    
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            final View v = inflater.inflate(R.layout.tab_frag3_layout, container, false);
            mListItems = new LinkedList<String>();
            mListItems.addAll(Arrays.asList(mNames));
            lyt = (PullAndLoadListView)v.findViewById(R.id.tab_frag3_listview1);
    
            if (container == null) {
                return null;
            }
    
            files = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,mListItems);
            lyt.setAdapter(files);
            lyt.setOnRefreshListener(new OnRefreshListener() {
    
                @Override
                public void onRefresh() {
                    // TODO Auto-generated method stub
                    new PullToRefreshDataTask().execute();
                }
            });
            lyt.setOnLoadMoreListener(new OnLoadMoreListener() {
    
                @Override
                public void onLoadMore() {
                    // TODO Auto-generated method stub
                    new LoadMoreDataTask().execute();
                }
            });
            return v;
    
        }
        private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                if (isCancelled()) {
                    return null;
                }
    
                // Simulates a background task
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
    
                for (int i = 0; i < mAnimals.length; i++)
                    mListItems.add(mAnimals[i]);
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                mListItems.add("Added after load more");
    
                // We need notify the adapter that the data have been changed
                files.notifyDataSetChanged();
    
                // Call onLoadMoreComplete when the LoadMore task, has finished
                lyt.onLoadMoreComplete();
    
                super.onPostExecute(result);
            }
    
            @Override
            protected void onCancelled() {
                // Notify the loading more operation has finished
                lyt.onLoadMoreComplete();
            }
        }
    
        private class PullToRefreshDataTask extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                if (isCancelled()) {
                    return null;
                }
    
                // Simulates a background task
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
    
                for (int i = 0; i < mAnimals.length; i++)
                    mListItems.addFirst(mAnimals[i]);
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                mListItems.addFirst("Added after pull to refresh");
    
                // We need notify the adapter that the data have been changed
                files.notifyDataSetChanged();
    
                // Call onLoadMoreComplete when the LoadMore task, has finished
                lyt.onRefreshComplete();
    
                super.onPostExecute(result);
            }
    
            @Override
            protected void onCancelled() {
                // Notify the loading more operation has finished
                lyt.onLoadMoreComplete();
            }
        }
    
    }
    

    这是的源代码 pull-to-refresh and load-more 图书馆

        2
  •  3
  •   Nicolas Jafelle    13 年前

    使用这个图书馆,我几天前就用过了,工作非常完美:

    RefreshableListView

        3
  •  1
  •   Darwind    13 年前

    我自己没有使用过这个库,它已经停产了(2个月前),但它看起来很好,有很多例子:

    https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide

    从我读到的内容来看,基本上你需要用库的listview替换你自己的listview,并导入jar文件,你就可以开始了;-)