Android:为 Android Market 等 Endless List 实现进度条和“正在加载...”

Posted

技术标签:

【中文标题】Android:为 Android Market 等 Endless List 实现进度条和“正在加载...”【英文标题】:Android: Implementing progressbar and "loading..." for Endless List like Android Market 【发布时间】:2011-06-07 17:07:14 【问题描述】:

android Market 获得灵感,我实现了一个 Endless List,当我们到达列表末尾时,它会从服务器加载更多数据。

现在,我需要实现进度条和“正在加载..”文本,如图所示

从中获取灵感的示例代码会很棒。

【问题讨论】:

我被困在同一个地方,谁能帮忙..***.com/questions/28122304/… 【参考方案1】:

这是一个解决方案,它还可以在加载时轻松地在 ListView 的末尾显示加载视图。

您可以在此处查看课程:

https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/helper/ListViewWithLoadingIndicatorHelper.java - 无需从 SimpleListViewWithLoadingIndicator 扩展即可使用功能的帮助器。

https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/listener/EndlessScrollListener.java - 当用户即将到达 ListView 底部时开始加载数据的监听器。

https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/view/SimpleListViewWithLoadingIndicator.java - EndlessListView。你可以直接使用这个类,也可以从它扩展。

【讨论】:

你能添加一些关于如何使用它的例子吗? 需要帮助***.com/questions/28122304/…【参考方案2】:

将onScrollListener 添加到 ListView。当用户滚动时,检查 ListView 是否接近尾声。如果是,则获取更多数据。举个例子:

public abstract class LazyLoader implements AbsListView.OnScrollListener 

    private static final int DEFAULT_THRESHOLD = 10 ;

    private boolean loading = true  ;
    private int previousTotal = 0 ;
    private int threshold = DEFAULT_THRESHOLD ;

    public LazyLoader() 

    public LazyLoader(int threshold) 
        this.threshold = threshold;
    

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

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) 
        if(loading) 
            if(totalItemCount > previousTotal) 
                // the loading has finished
                loading = false ;
                previousTotal = totalItemCount ;
            
        

        // check if the List needs more data
        if(!loading && ((firstVisibleItem + visibleItemCount ) >= (totalItemCount - threshold))) 
            loading = true ;

            // List needs more data. Go fetch !!
            loadMore(view, firstVisibleItem,
                    visibleItemCount, totalItemCount);
        
    

    // Called when the user is nearing the end of the ListView
    // and the ListView is ready to add more items.
    public abstract void loadMore(AbsListView view, int firstVisibleItem,
                                  int visibleItemCount, int totalItemCount);

活动:

public class MainActivity extends AppCompatActivity 

        @Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main_layout);
            ListView listView = (ListView) findViewById(R.id.listView);

            listView.setOnScrollListener(new LazyLoader() 
                @Override
                public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
                    // Fetch your data here !!!
                
            );
        
    

你可以找到完整的实现at this link

【讨论】:

【参考方案3】:

这里的其他答案是指过时的、未维护的解决方案。然而,这篇文章似乎是最新的: https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView

有太多东西可以把它全部放在一个 SO 答案中,但在我写这个答案时,这里有一些重要的部分:

RecyclerView 实现无限分页需要以下步骤:

    将EndlessRecyclerViewScrollListener.java 复制到您的应用程序中。 在RecyclerView 上调用addOnScrollListener(...) 以启用无限分页。传入EndlessRecyclerViewScrollListener 的实例并实现onLoadMore,每当需要加载新页面以填满列表时就会触发。 在上述onLoadMore 方法中,通过发送网络请求或从其他来源加载将其他项目加载到适配器中。

要开始处理第 2 步和第 3 步的滚动事件,我们需要在 ActivityFragment 中使用 addOnScrollListener() 方法,并使用布局管理器传入 EndlessRecyclerViewScrollListener 的实例,如下所示:

public class MainActivity extends Activity 
    // Store a member variable for the listener
    private EndlessRecyclerViewScrollListener scrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
       // Configure the RecyclerView
       RecyclerView rvItems = (RecyclerView) findViewById(R.id.rvContacts);
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
       rvItems.setLayoutManager(linearLayoutManager);
       // Retain an instance so that you can call `resetState()` for fresh searches
       scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) 
           @Override
           public void onLoadMore(int page, int totalItemsCount, RecyclerView view) 
               // Triggered only when new data needs to be appended to the list
               // Add whatever code is needed to append new items to the bottom of the list
               loadNextDataFromApi(page);
           
      ;
      // Adds the scroll listener to RecyclerView
      rvItems.addOnScrollListener(scrollListener);
  

  // Append the next page of data into the adapter
  // This method probably sends out a network request and appends new data items to your adapter. 
  public void loadNextDataFromApi(int offset) 
      // Send an API request to retrieve appropriate paginated data 
      //  --> Send the request including an offset value (i.e `page`) as a query parameter.
      //  --> Deserialize and construct new model objects from the API response
      //  --> Append the new data objects to the existing set of items inside the array of items
      //  --> Notify the adapter of the new items made with `notifyItemRangeInserted()`
  

【讨论】:

以上是关于Android:为 Android Market 等 Endless List 实现进度条和“正在加载...”的主要内容,如果未能解决你的问题,请参考以下文章

转发布android app到android market的方法

如何为Android Market和App Store制作二维码[关闭]

android market

怎么注册android market的 帐户

Android Market URL格式

Android Market URL子字符串搜索