简体   繁体   English

Android:在“水平”列表视图中添加“加载更多项目”

[英]Android : Add Load more items in Horizontal listview

I am using horizontal list view and adding dynamic data into this, but I want to add an item when the scroll reached to the right most item in the listview . 我正在使用水平列表视图,并向其中添加动态数据,但是当滚动到listview最右边的项目时,我想添加一个项目。 How can I get that scroll reached to the right most position? 如何使滚动条到达最右边?

Implement an onScrollListener to your listview, and fetch data through onScroll() method. 实现一个onScrollListener到您的列表视图,并通过onScroll()方法获取数据。 There are many libraries for doing this easily. 有许多库可以轻松做到这一点。 The following is a vertical listview example, but you can always change it to Horizontal and modify the code according to your needs. 以下是垂直列表视图的示例,但是您始终可以将其更改为“水平”,并根据需要修改代码。

https://github.com/shontauro/android-pulltorefresh-and-loadmore https://github.com/shontauro/android-pulltorefresh-and-loadmore

In order to do that, you have to mock the size of the list ruled by your adapter. 为此,您必须模拟适配器所规定的列表大小。 For example, if the list is called myList, its size is myList.size(), you will have to do something like: myList.add(mockelemnt). 例如,如果列表名为myList,其大小为myList.size(),则必须执行以下操作:myList.add(mockelemnt)。

Now that you have mocked the last element, what you may do is to handle it on your adapter method getView(): 既然您已经模拟了最后一个元素,则可以在适配器方法getView()上处理它:

@Override
   public View getView(int position, View convertView, ViewGroup parent){
        if (oistion < myList.size(){
           //work with conventional view
        }else if (postion == myList.size()){
           //work with addMore view
      }

} }

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM