zoukankan      html  css  js  c++  java
  • 加载更多和刷新加载问题总结

    对于加载更多问题可以有很多种,目前我们前面的平台采用的是以下方式加载更多,话不多说,直接附上代码……

    public class CompanyView {

    private ArrayList<Shop> shops, addShops;
        private DataParser parser;
        private ListView companyList;
        private ShopListAdapter adapter;
        private Context ctx;
        private int page = 1;// 页码
        private View moreView;// 加载更多
        private TextView moreText;
        private int visibleLastIndex;// 可见的最后一项
        private int visibleCount;// 可见的数目

        public void setContent(Context ctx, View lay) {
            this.ctx = ctx;
            companyList = (ListView) lay.findViewById(R.id.category_shop_list);
            if (CheckNet.netIsAvaliable(ctx)) {
                new AsynLoad(ctx).execute(Config.CATEGORYACTIVITY_MSG);
            }
            companyList.setOnItemClickListener(itemListener);
            moreView = ((CategoryActivity) ctx).getLayoutInflater().inflate(
                    R.layout.add_more_layout, null);
            companyList.addFooterView(moreView);
            moreText = (TextView) moreView.findViewById(R.id.add_more_text);
            moreView.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    page++;
                    addMore();
                }
            });
            companyList.setOnScrollListener(listScrollListener);
        }

        // 从网络上获取数据
        public void initData() {
            parser = new DataParser();
            shops = parser.parserShops(Constant.replacePage(1));
        }

        // 为Listview装载数据
        public void setData() {
            if (shops != null) {
                adapter = new ShopListAdapter(ctx, shops);
                companyList.setAdapter(adapter);
            }
        }

        private AdapterView.OnItemClickListener itemListener = new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // TODO Auto-generated method stub
                Intent i = new Intent(ctx, ShopActivity.class);
                Bundle data = new Bundle();
                data.putSerializable("shop", shops.get(position));
                i.putExtras(data);
                ctx.startActivity(i);
            }
        };

        // 加载更多
        private void addMore() {
            moreText.setText("加载中......");
            new Handler().postDelayed(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub
                    boolean flag = loadMore();
                    adapter.notifyDataSetChanged();
                    companyList.setSelection(visibleLastIndex - visibleCount + 1);
                    if (flag) {
                        moreText.setText("更多");
                    } else {
                        moreText.setText("加载完毕了");
                    }
                }
            }, 2000);
        }

        // 加载数据
        private boolean loadMore() {
            String url;
            parser = new DataParser();
            url = Constant.replacePage(page);// 页码替换
            Log.i("more", url);
            addShops = parser.parserShops(url);
            Log.i("page===============>", "" + page);
            if (addShops == null || addShops.size() == 0) {
                return false;
            } else {
                for (Shop shop : addShops) {
                    adapter.addShop(shop);
                }
                return true;
            }
        }

        private OnScrollListener listScrollListener = new OnScrollListener() {
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                visibleCount = visibleItemCount;
                visibleLastIndex = visibleItemCount + firstVisibleItem - 1;
            }
        };

    }


    END
    ---------------------------------------------------------------------------------------------
    欢迎关注 我的微博@疯狂的迈步 我的github@junhey
  • 相关阅读:
    gnome3 修改桌面背景图片模式
    记录openSUSE 源码安装node.js
    [转]gnome环境中将家目录下预设的文件夹由中文名称改为英文名称
    Clover config.plist Boot部分
    bootstrap table 实现固定悬浮table 表头并可以水平滚动
    openSUSE 安装compass,mkmf.rb can't find,checking for ffi.h...extconf.rb failed
    读《深入PHP 面向对象、模式与实践》笔记
    openSUSE中启用apache mod_rewrite
    openSUSE安装Composer
    openSUSE 安装LAMP记录
  • 原文地址:https://www.cnblogs.com/junhey/p/3559117.html
Copyright © 2011-2022 走看看