简体   繁体   中英

SwipeRefreshLayout only shows one item?

I've tried implementing a SwipeRefreshLayout for an existing ListView, but whenever I run the program, it only shows one item even when I refresh it by pulling. The refresh animation works, however. Here is the relevant code:

    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);
        }
    });

The problem wasn't with the SwipeRefreshLayout. I put a ListView in a ScrollView in the xml, leading to the error.

只需删除滚动视图,列表视图就足够了

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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