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
  • 相关阅读:
    android 的权限
    做android遇到有问题有感
    帮人写的 论文 C语言的 学生管理系统
    android 服务器的 mysql 查询悲剧
    android开发遇到的问题
    想和各位技术高人材交流技术特建了相关的QQ群
    Invalid token '44' in input string
    设置PLSQL Developer访问本机64位Oracle
    SQL Server 2005还原数据库时出现“不能选择文件或文件组XXX_log用于此操作……错误:3219……”的解决方法
    C#的JSON开发包 LitJSON
  • 原文地址:https://www.cnblogs.com/junhey/p/3559117.html
Copyright © 2011-2022 走看看