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
359 0
|
6月前
|
前端开发
微信拖拽盒子movable-view点击事件失效问题
微信拖拽盒子movable-view点击事件失效问题
|
JavaScript API 开发者
uniapp滚动加载 下拉刷新
在日常开发中,滚动加载和下拉刷新是非常常见的功能,页面数据过多时,需要滚动加载优化性能,本篇技术分享博客将介绍如何在uniapp中实现滚动加载和下拉刷新。至此,我们已经成功地实现了滚动加载和下拉刷新两种常见的移动端功能。大家可以进行扩充或者留言交流!通过以上示例代码,我们可以看到uniapp提供了非常方便的API来实现这些功能,使得开发者可以更加专注于业务逻辑的实现。
248 0
|
小程序
小程序-uniApp:区域滚动、下拉刷新、上拉加载更多
小程序-uniApp:区域滚动、下拉刷新、上拉加载更多
885 0
|
JavaScript
DOM ------ 仿淘宝关闭二维码
DOM ------ 仿淘宝关闭二维码
|
小程序 API
零基础学小程序007----首页轮播图,可以自动轮播,循环轮播,定时轮播
零基础学小程序007----首页轮播图,可以自动轮播,循环轮播,定时轮播
110 0
|
Dart 开发者
【Flutter】底部导航栏页面框架 ( BottomNavigationBar 底部导航栏 | PageView 滑动页面 | 底部导航与滑动页面关联操作 )(二)
【Flutter】底部导航栏页面框架 ( BottomNavigationBar 底部导航栏 | PageView 滑动页面 | 底部导航与滑动页面关联操作 )(二)
154 0
【Flutter】底部导航栏页面框架 ( BottomNavigationBar 底部导航栏 | PageView 滑动页面 | 底部导航与滑动页面关联操作 )(二)