【问题标题】:Android - PullToRefresh ListView always emptyAndroid - PullToRefresh ListView 始终为空
【发布时间】:2013-06-16 01:06:48
【问题描述】:

我正在尝试在我的应用中实现 chrisbanes's Android-PullToRefresh。 但到目前为止,我得到的只是一个空洞的观点。 如果我将 ListView 更改为 Android 小部件,它可以正常工作,如果我使用 PullToRefreshListView,则列表显示为空。 我正在使用自定义 ListAdapter。

这是 XML 部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/listview_jogos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null" >
    </com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>

这是活动中的代码(它被调用,至少根据调试信息):

private void constroiLista(ArrayList<Jogo> lista)
{
    PullToRefreshListView lv = (PullToRefreshListView)findViewById(R.id.listview_jogos);
    if(lv != null && lista != null)
    {
        lv.setAdapter(new ListAdapter(this, lista));
        lv.setOnRefreshListener(new OnRefreshListener<ListView>()
        {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView)
            {
                (dnt = new DownloadJogosTask()).execute();
            }
        });
        // Call onRefreshComplete when the list has been refreshed.
        lv.onRefreshComplete();
    }
}

如果它有所不同,包含此列表视图的布局将被膨胀为基础布局:

((LinearLayout)findViewById(R.id.ll_base_layout)).addView(View.inflate(this, R.layout.lista_jogos, null));

是否有任何与此库相关的已知错误?我四处寻找,但没有找到类似的东西。提前感谢您的帮助:)

【问题讨论】:

    标签: android android-listview listadapter pull-to-refresh


    【解决方案1】:

    在尝试了 Tarek 的建议后,我注意到 PullToRefreshListView 的高度始终为 0。

    如果我使用setContentView 设置布局而不是将其膨胀到另一个布局中......它可以工作,或者如果我设置了固定高度new LayoutParams(LayoutParams.MATCH_PARENT, 1000)

    在搞砸之后,我最终在我的 customOnCreate 方法中添加了这段代码以使其工作:

    ((LinearLayout)findViewById(R.id.ll_base_layout)).addView(View.inflate(this, R.layout.lista_jogos, null));
    LinearLayout ll = (LinearLayout)findViewById(R.id.ll_jogos);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ll.getLayoutParams();
    params.weight = 1;
    ll.setLayoutParams(params);
    

    感谢您的帮助:)

    【讨论】:

      【解决方案2】:

      我假设您的(dnt = new DownloadJogosTask()).execute() 会下载Jogos 并将它们添加到lista

      如果这是您的情况,请尝试以下方法:

      ListAdapter la = null; //make your ListAdapter global in your activity
      PullToRefreshListView lv = null; //also the same for the list
      
      private void constroiLista(ArrayList<Jogo> lista) {
      
          lv = (PullToRefreshListView) findViewById(R.id.listview_jogos);
          la = new ListAdapter(this, lista); // initialize the list adapter
          lv.setAdapter(la);
      
          lv.setOnRefreshListener(new OnRefreshListener<ListView>() {
              @Override
              public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                  if (lv != null && lista != null) { //if null do not execute the task
                      (dnt = new DownloadJogosTask()).execute();
                  }
              }
          });
          // here is not when the list refresh is complete so remove this line
      
      }
      

      DownloadJogosTaskonPostExecute 中,您调用la.notifyDataSetChanged(); 以通知适配器您已将新值添加到您的lista,然后调用lv.onRefreshComplete();,这只会隐藏加载动画。

      @Override
      protected void onPostExecute(Void result) {
          super.onPostExecute(result);
          la.notifyDataSetChanged();
          lv.onRefreshComplete();
      }
      

      希望对您有所帮助。

      【讨论】:

      • 它没有用,但帮助我注意到 PullToRefreshListView 的高度始终为 0。无论如何谢谢 :)
      【解决方案3】:

      从 PullToRefreshListView 中的 wrap_content 替换 match_parent

       <com.handmark.pulltorefresh.library.PullToRefreshListView
              android:id="@+id/listview_jogos"
              android:layout_width="match_parent"
              android:layout_height="match_parent" <= Replace This Attribute
              android:divider="@null" >
          </com.handmark.pulltorefresh.library.PullToRefreshListView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多