0

我已经尝试为现有的 ListView 实现 SwipeRefreshLayout,但是每当我运行程序时,即使我通过拉动刷新它,它也只显示一个项目。但是,刷新动画有效。以下是相关代码:

    items = (ListView)findViewById(R.id.myList);
    adapter = new Adapter(ctx);
    adapter.setTextKey("myItem");
    //adapter.setImageKey("image");
    items.setAdapter(adapter);
    adapter.loadObjects();

    final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe2);
    swipeView.setEnabled(false);
    swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeView.setRefreshing(true);
            ( new Handler()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeView.setRefreshing(false);

                }
            }, 3000);
        }
    });

    items.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            boolean enable = false;
            if(items != null && items.getChildCount() > 0){
                // check if the first item of the list is visible
                boolean firstItemVisible = items.getFirstVisiblePosition() == 0;
                // check if the top of the first item is visible
                boolean topOfFirstItemVisible = items.getChildAt(0).getTop() == 0;
                // enabling or disabling the refresh layout
                enable = firstItemVisible && topOfFirstItemVisible;
            }
            swipeView.setEnabled(enable);
        }
    });
4

2 回答 2

1

问题不在于 SwipeRefreshLayout。我在xml中的一个ScrollView中放了一个ListView,导致报错。

于 2014-08-01T04:12:33.367 回答
0

去掉scrollview,listview就够了

于 2015-08-13T09:10:52.580 回答