Android - 下拉刷新 ListView 总是为空

3
我正在尝试在我的应用程序中实现chrisbanes的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));

这个库是否存在已知的错误?我搜索过,但没有找到类似的情况。提前感谢你的帮助 :)
3个回答

4

在尝试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);

感谢你的帮助 :)

3
我假设你的 (dnt = new DownloadJogosTask()).execute() 会下载游戏并将它们添加到列表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。无论如何还是谢谢你 :) - Marco Batista

2

在PullToRefreshListView中将match_parent替换为wrap_content

 <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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接