列表视图正在使用不正确执行负载更多的功能 [英] Listview is working incorrectly with implementing a load more function

查看:79
本文介绍了列表视图正在使用不正确执行负载更多的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个列表视图,并从外部数据库中获取它的数据。我想有20个项目在第一时间,那么如果用户向下滚动它加载另一个20等。

So, I have a listview and getting the data for it from an external database. I would like to have 20 items the first time, then if the user scrolls down it loads another 20 and so on.

class ItemAdapter extends BaseAdapter {
        private ArrayList<Item> objects;
        private class ViewHolder {
             public TextView text_tt;  

        }

        @Override
        public int getCount() {
            return SIZE;
            //return 9;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        } 

@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            final ViewHolder viewHolder;
            if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.text_tt = (TextView) v.findViewById(R.id.toptext);
            } else {
            viewHolder = (ViewHolder) convertView.getTag();
            }

            if(position==getCount()-1){
                LoadMore(); //asynctask - load other 20

            }
            return v;
        }
    }

在荷载作用我解析pre-读取JSON数据,所以我只需要再添20一个列表, notifyDataSetChanged()。该功能工作良好,但它有一个副作用:约50%的上​​的项目的点击不被识别的时间。我向下滚动,接收下一个20,但我不能点击任何物品。但是,例如,如果我改变的活动,回到列表视图,它的工作原理。为什么? 谢谢!

In the load function I parse the pre-read json data so i just have to add another 20 one to the list and notifyDataSetChanged().. The function works well but it has a side effect: about 50% of the time the click on items is not recognized. I scroll down, receive the next 20 but I cannot click on any items. But for example if I change activity and come back to the listview, it works. Why? Thank you!

推荐答案

您可以设置 OnScrollListener 的ListView 。在 onScroll()的方法,检查:

You can set a OnScrollListener on your ListView. In the onScroll() method, check if:

firstVisible + visibleItemCount = totalItemCount

如果该条件满足,就可以从数据库项目的最新名单装入多个项目,重新初始化适配器。

If this condition is satisfied, you can load more items from the database, reinitialize the adapter with the updated list of items.

listView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {}

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if ((firstVisibleItem + visibleItemCount) == totalItemCount) {

                // add 20 more items to the list and reinitialize the adapter

            } 
        }
    });
}

这篇关于列表视图正在使用不正确执行负载更多的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆