IT资讯----------上拉加载,下拉刷新和缓存

简介: 该App已经上传到百度应用市场:http://shouji.baidu.com/soft/item?docid=8928185&from=as&f=search_app_it%E8%B5%84%E8%AE%AF%40list_1_image%402%40header_all_input 有兴趣的可以下载看看。接下来我会公布源代码,不过该App并没有使用代码


该App已经上传到百度应用市场:http://shouji.baidu.com/soft/item?docid=8928185&from=as&f=search_app_it%E8%B5%84%E8%AE%AF%40list_1_image%402%40header_all_input

有兴趣的可以下载看看。接下来我会公布源代码,不过该App并没有使用代码混淆,所以可以通过反编译清楚的看到源码。


首先是对MainFragment.java编写,真对XListView实现下拉刷新和上拉加载。

@Override
public void onLoadMore() {
    new LoadDatasTask().execute(LOAD_MORE);
}

@Override
public void onRefresh() {
    new LoadDatasTask().execute(LOAD_REFREASH);
}

我们对下拉刷新和上拉加载调用了一个异步任务,传入了操作的标识符,下面具体看个类:


  1. /**
     * 记载数据的异步任
     *
     * @author
     */
    class LoadDatasTask extends AsyncTask<Integer, Void, Integer> {
        @Override
        protected Integer doInBackground(Integer... params) {
            switch (params[0]) {
                case LOAD_MORE:
                    loadMoreData();
                    break;
                case LOAD_REFREASH:
                    refreashData();
                    break;
            }
            return -1;
        }
    
        @Override
        protected void onPostExecute(Integer result) {
            switch (result) {
                case TIP_ERROR_NO_NETWORK:
                    Toast.makeText(getActivity(), "没网络连接", Toast.LENGTH_SHORT).show();
                    mAdapter.notifyDataSetChanged();
                    mXlistView.stopRefresh();
                    break;
                case TIP_ERROR_SERVER:
                    Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            switch (select) {
                case 1:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes));
                    break;
                case 2:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_cto));
                    break;
                case 3:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_blog_house));
                    break;
                case 4:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_iteye));
                    break;
            }
            mXlistView.stopRefresh();
            mXlistView.stopLoadMore();
        }
    }
可以看到,这个类中又使用了  

loadMoreData();和 refreashData();

先看loadMoreData:


/**
 * 根据网络决定是从哪里取数据
 */
public void loadMoreData() {
    System.out.println("isLoadingDataFromNetwork------" + isLoadingDataFromNetwork);
    if (isLoadingDataFromNetwork) {
        currentPage =currentPage+ 1;
        List<NewsItem> newsItems = null;
        try {
            switch (select) {
                case 1:
                    newsItems = newsItemBiz.getNewsItems(newTypes, currentPage);
                    break;
                case 2:
                    newsItems = newItem51CTO.getNewsItems(newTypes_cto, currentPage);
                    break;
                case 3:
                    newsItems = blogHouse.getNewsItems(newTypes_blog_house, currentPage);
                    break;
                case 4:
                    newsItems = iTeye.getNewsItems(newTypes_iteye, currentPage,newTypes_iteye);
                    break;
            }
            newsItemDao.add(newsItems,select);
            mAdapter.addAll(newsItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        // 从数据库加载的
        currentPage += 1;
        List<NewsItem> newsItems = null;
        switch (select) {
            case 1:
                newsItems = newsItemDao.list(newTypes, currentPage,select);
                break;
            case 2:
                newsItems = newsItemDao.list(newTypes_cto, currentPage,select);
                break;
            case 3:
                newsItems = newsItemDao.list(newTypes_blog_house, currentPage,select);
                break;
            case 4:
                newsItems = newsItemDao.list(newTypes_iteye, currentPage,select);
                break;
        }
        if(newsItems!=null) {
            mAdapter.addAll(newsItems);
        }
    }
}

可以看到,如果当前app上的数据是从网络更新的即最新数据,我们会继续去网络加载下一页的数据,并且使用mNewsItemDao.add方法,插入我们的app的数据库中,供没有网络时使用。如果当前数据是缓存数据,我们则继续去数据库加载。

接下来看refreashData方法:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**
     * 刷新获取数据
     *
     * @return
     */
    public Integer refreashData() {
        if (NetUtil.checkNet(getActivity())) {
            isConnNet = true;
            List<NewsItem> newsItems = null;
            try {
                switch (select) {
                    case 1:
                        newsItems = newsItemBiz.getNewsItems(newTypes, currentPage);
                        List<NewsItem> newsItem1 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem1.add(newsItems.get(i));
                        }
    
                        mAdapter.setDatas(newsItem1);
                        mDatas=newsItem1;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes,select);
                        // 存入数据库
                        newsItemDao.add(newsItems,select);
                        break;
                    case 2:
                        newsItems = newItem51CTO.getNewsItems(newTypes_cto, currentPage);
                        List<NewsItem> newsItem2 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem2.add(newsItems.get(i));
                        }
                        mAdapter.setDatas(newsItem2);
                        mDatas=newsItem2;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes_cto);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes_cto,select);
                        // 存入数据库
                        newsItemDao.add(newsItem2,select);
                        break;
                    case 3:
                        newsItems = blogHouse.getNewsItems(newTypes_blog_house, currentPage);
                        List<NewsItem> newsItem3 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem3.add(newsItems.get(i));
                        }
                        mAdapter.setDatas(newsItem3);
                        mDatas=newsItem3;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes_blog_house);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes_blog_house,select);
                        // 存入数据库
                        newsItemDao.add(newsItem3,select);
                        break;
                    case 4:
                        newsItems = iTeye.getNewsItems(newTypes_iteye, currentPage, newTypes_iteye);
                        List<NewsItem> newsItem4 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem4.add(newsItems.get(i));
                        }
                        mAdapter.setDatas(newsItem4);
                        mDatas=newsItem4;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes_iteye);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes_iteye,select);
                        // 存入数据库
                        newsItemDao.add(newsItem4,select);
                        break;
                }
              //  mDatas = newsItems;
            } catch (Exception e) {
                e.printStackTrace();
                isLoadingDataFromNetwork = false;
                return TIP_ERROR_SERVER;
            }
        } else {
            isConnNet = false;
            isLoadingDataFromNetwork = false;
            List<NewsItem> newsItems = null;
            switch (select) {
                case 1:
                    //从数据库取数据
                    newsItems = newsItemDao.list(newTypes, currentPage,select);
                    break;
                case 2:
                    newsItems = newsItemDao.list(newTypes_cto, currentPage,select);
                    break;
                case 3:
                    newsItems = newsItemDao.list(newTypes_blog_house, currentPage,select);
                    break;
                case 4:
                    newsItems = newsItemDao.list(newTypes_iteye, currentPage,select);
                    break;
            }
            mDatas = newsItems;
            return TIP_ERROR_NO_NETWORK;
        }
        return -1;
    }
    
首先检查网络连接情况,没有的话,则去数据库加载,并设置isLoadingDataFromNetWork=false,如果有的话从网络加载,并清空该类型新闻的数据,更新数据库缓存数据。

下面是完整MainFragment.java:


public class MainFragment extends Fragment implements IXListViewRefreshListener, IXListViewLoadMore {

    private static final int LOAD_MORE = 0x110;
    private static final int LOAD_REFREASH = 0x111;

    private static final int TIP_ERROR_NO_NETWORK = 0x112;
    private static final int TIP_ERROR_SERVER = 0x113;

    //是否是第一次进入
    private boolean isFirstIn = true;
    //是否连接网络
    private boolean isConnNet = false;

    //选择是选择哪个博客  1:CSDN  2:51CTO   3:博客园    4:ITEYE博客
    private int select;
    //选择的是博客的哪个
    private int selectType;
    /**
     * 当前数据是否从网络获取
     */
    private boolean isLoadingDataFromNetwork=true;
    //取CSDN数据
    private int newTypes = Constant.NEW_TYPE_YEJIE;
    //取51CTO数据
    private int newTypes_cto = Constant.NEWS_TYPE_NETWORK;
    //取博客园数据
    private int newTypes_blog_house = Constant.NEWS_TYPE_HOME;
    //取ITeye数据
    private int newTypes_iteye = Constant.NEWS_TYPE_NEW;
    private int currentPage = 1;
    //CSDN
    private NewsItemBiz newsItemBiz;
    //51CTO
    private NewItem51CTO newItem51CTO;
    //博客园
    private NewItemBlogHouse blogHouse;
    //ITeye
    private NewItemITeye iTeye;
    private me.maxwin.view.XListView mXlistView;
    private NewsItemAdapter mAdapter;
    private List<NewsItem> mDatas = new ArrayList<NewsItem>();
    /**
     * 与数据库交互
     */
    private NewsItemDao newsItemDao;

    public MainFragment() {
    }

    public MainFragment(int newsType, int select) {
        this.newTypes = newsType;
        this.newTypes_cto = newsType + 5;
        this.newTypes_blog_house = newsType + 9;
        this.newTypes_iteye = newsType + 13;
        this.select = select;
        newsItemBiz = new NewsItemBiz();
        newItem51CTO = new NewItem51CTO();
        blogHouse = new NewItemBlogHouse();
        iTeye = new NewItemITeye();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_item_fragment_main, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        newsItemDao = new NewsItemDao(getContext());
        mAdapter = new NewsItemAdapter(getActivity(), mDatas, select);
        mAdapter.setSelectType(newTypes_iteye);
        mXlistView = (me.maxwin.view.XListView) getView().findViewById(R.id.id_xlistView);
        mXlistView.setAdapter(mAdapter);
        mXlistView.setPullRefreshEnable(this);
        mXlistView.setPullLoadEnable(this);
        switch (select) {
            case 1:
                mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes));
                break;
            case 2:
                mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_cto));
                break;
            case 3:
                mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_blog_house));
                break;
            case 4:
                mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_iteye));
                break;
        }
        // mXlistView.startRefresh();
        mXlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NewsItem newsItem = mDatas.get(position-1);
                NewsItem nextItem=mDatas.get(position);
                Intent intent=null;
                if (newTypes_iteye==17){
                    intent = new Intent(getActivity(), SubjectsActivity.class);
                    intent.putExtra("url", newsItem.getLink());
                }else {
                    intent = new Intent(getActivity(), NewsContentActivity.class);
                    intent.putExtra("url", newsItem.getLink());
                    intent.putExtra("nextUrl", nextItem.getLink());
                    intent.putExtra("select", select);
                    intent.putExtra("cuttenrPage", currentPage);
                }
                startActivity(intent);
            }
        });
        if (isFirstIn) {
            /**
             * 进来时直接刷新
             */
            mXlistView.startRefresh();
            isFirstIn = false;
        } else {
            mXlistView.NotRefreshAtBegin();
        }
    }

    @Override
    public void onLoadMore() {
        new LoadDatasTask().execute(LOAD_MORE);
    }

    @Override
    public void onRefresh() {
        new LoadDatasTask().execute(LOAD_REFREASH);
    }

    /**
     * 记载数据的异步任
     *
     * @author
     */
    class LoadDatasTask extends AsyncTask<Integer, Void, Integer> {
        @Override
        protected Integer doInBackground(Integer... params) {
            switch (params[0]) {
                case LOAD_MORE:
                    loadMoreData();
                    break;
                case LOAD_REFREASH:
                    refreashData();
                    break;
            }
            return -1;
        }

        @Override
        protected void onPostExecute(Integer result) {
            switch (result) {
                case TIP_ERROR_NO_NETWORK:
                    Toast.makeText(getActivity(), "没网络连接", Toast.LENGTH_SHORT).show();
                    mAdapter.notifyDataSetChanged();
                    mXlistView.stopRefresh();
                    break;
                case TIP_ERROR_SERVER:
                    Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            switch (select) {
                case 1:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes));
                    break;
                case 2:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_cto));
                    break;
                case 3:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_blog_house));
                    break;
                case 4:
                    mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_iteye));
                    break;
            }
            mXlistView.stopRefresh();
            mXlistView.stopLoadMore();
        }
    }

    /**
     * 刷新获取数据
     *
     * @return
     */
    public Integer refreashData() {
        if (NetUtil.checkNet(getActivity())) {
            isConnNet = true;
            List<NewsItem> newsItems = null;
            try {
                switch (select) {
                    case 1:
                        newsItems = newsItemBiz.getNewsItems(newTypes, currentPage);
                        List<NewsItem> newsItem1 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem1.add(newsItems.get(i));
                        }

                        mAdapter.setDatas(newsItem1);
                        mDatas=newsItem1;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes,select);
                        // 存入数据库
                        newsItemDao.add(newsItems,select);
                        break;
                    case 2:
                        newsItems = newItem51CTO.getNewsItems(newTypes_cto, currentPage);
                        List<NewsItem> newsItem2 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem2.add(newsItems.get(i));
                        }
                        mAdapter.setDatas(newsItem2);
                        mDatas=newsItem2;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes_cto);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes_cto,select);
                        // 存入数据库
                        newsItemDao.add(newsItem2,select);
                        break;
                    case 3:
                        newsItems = blogHouse.getNewsItems(newTypes_blog_house, currentPage);
                        List<NewsItem> newsItem3 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem3.add(newsItems.get(i));
                        }
                        mAdapter.setDatas(newsItem3);
                        mDatas=newsItem3;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes_blog_house);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes_blog_house,select);
                        // 存入数据库
                        newsItemDao.add(newsItem3,select);
                        break;
                    case 4:
                        newsItems = iTeye.getNewsItems(newTypes_iteye, currentPage, newTypes_iteye);
                        List<NewsItem> newsItem4 = new ArrayList<>();
                        for (int i = 0; i <= newsItems.size() / 2; i++) {
                            newsItem4.add(newsItems.get(i));
                        }
                        mAdapter.setDatas(newsItem4);
                        mDatas=newsItem4;
                        isLoadingDataFromNetwork = true;
                        // 设置刷新时间
                        AppUtil.setRefreashTime(getActivity(), newTypes_iteye);
                        // 清除数据库数据
                        newsItemDao.deleteAll(newTypes_iteye,select);
                        // 存入数据库
                        newsItemDao.add(newsItem4,select);
                        break;
                }
              //  mDatas = newsItems;
            } catch (Exception e) {
                e.printStackTrace();
                isLoadingDataFromNetwork = false;
                return TIP_ERROR_SERVER;
            }
        } else {
            isConnNet = false;
            isLoadingDataFromNetwork = false;
            List<NewsItem> newsItems = null;
            switch (select) {
                case 1:
                    //从数据库取数据
                    newsItems = newsItemDao.list(newTypes, currentPage,select);
                    break;
                case 2:
                    newsItems = newsItemDao.list(newTypes_cto, currentPage,select);
                    break;
                case 3:
                    newsItems = newsItemDao.list(newTypes_blog_house, currentPage,select);
                    break;
                case 4:
                    newsItems = newsItemDao.list(newTypes_iteye, currentPage,select);
                    break;
            }
            mDatas = newsItems;
            return TIP_ERROR_NO_NETWORK;
        }
        return -1;
    }

    /**
     * 根据网络决定是从哪里取数据
     */
    public void loadMoreData() {
        System.out.println("isLoadingDataFromNetwork------" + isLoadingDataFromNetwork);
        if (isLoadingDataFromNetwork) {
            currentPage =currentPage+ 1;
            List<NewsItem> newsItems = null;
            try {
                switch (select) {
                    case 1:
                        newsItems = newsItemBiz.getNewsItems(newTypes, currentPage);
                        break;
                    case 2:
                        newsItems = newItem51CTO.getNewsItems(newTypes_cto, currentPage);
                        break;
                    case 3:
                        newsItems = blogHouse.getNewsItems(newTypes_blog_house, currentPage);
                        break;
                    case 4:
                        newsItems = iTeye.getNewsItems(newTypes_iteye, currentPage,newTypes_iteye);
                        break;
                }
                newsItemDao.add(newsItems,select);
                mAdapter.addAll(newsItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // 从数据库加载的
            currentPage += 1;
            List<NewsItem> newsItems = null;
            switch (select) {
                case 1:
                    newsItems = newsItemDao.list(newTypes, currentPage,select);
                    break;
                case 2:
                    newsItems = newsItemDao.list(newTypes_cto, currentPage,select);
                    break;
                case 3:
                    newsItems = newsItemDao.list(newTypes_blog_house, currentPage,select);
                    break;
                case 4:
                    newsItems = newsItemDao.list(newTypes_iteye, currentPage,select);
                    break;
            }
            if(newsItems!=null) {
                mAdapter.addAll(newsItems);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainFragment");
    }
}


目录
相关文章
|
小程序 前端开发 JavaScript
微信小程序触底加载scroll-view
微信小程序触底加载scroll-view
385 0
|
5月前
uniapp实战 —— 猜你喜欢(含滚动触底分页加载)
uniapp实战 —— 猜你喜欢(含滚动触底分页加载)
75 3
|
5月前
|
JSON 小程序 数据库
【微信小程序-原生开发】实用教程14 - 列表的分页加载,触底加载更多(含无更多数据的提醒和显示,自定义组件)
【微信小程序-原生开发】实用教程14 - 列表的分页加载,触底加载更多(含无更多数据的提醒和显示,自定义组件)
146 0
|
7月前
|
小程序
uniapp-微信小程序-上拉和下拉触底刷新
uniapp-微信小程序-上拉和下拉触底刷新
157 0
|
7月前
|
小程序
【微信小程序】-- 全局配置 -- window - 下拉刷新 & 上拉触底(十六)
【微信小程序】-- 全局配置 -- window - 下拉刷新 & 上拉触底(十六)
|
7月前
|
JSON 小程序 JavaScript
【微信小程序】-- 页面事件 - 下拉刷新(二十五)
【微信小程序】-- 页面事件 - 下拉刷新(二十五)
|
7月前
|
小程序
【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)
【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)
|
7月前
|
前端开发
微信拖拽盒子movable-view点击事件失效问题
微信拖拽盒子movable-view点击事件失效问题
|
7月前
|
JavaScript
原生js如何实现上拉加载下拉刷新?
原生js如何实现上拉加载下拉刷新?
48 0