检测ListView Scroll的顶部

时间:2014-09-04 06:51:58

标签: android android-listview

我想检测列表顶部视图,我正在使用此方法。

@Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0)
                    swipeRefreshLayout.setEnabled(true);
                else
                    swipeRefreshLayout.setEnabled(false);
            }

这很好用,但问题是我已将Header View附加到列表中。当我向上滚动时,只要第一个项目可见(而不是标题视图),它就会调用pull来刷新列表视图。如何检测List的标题是否完全可见。 我的列表视图是

View imageSlider = inflater.inflate(R.layout.image_slider_layout, null, false);
findViewById(imageSlider);

mPullRefreshListView.addHeaderView(imageSlider);

private void findViewById(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
    }

2 个答案:

答案 0 :(得分:4)

嗯,有趣的答案在于onTouch监听器而不是onScroll,请参阅下面的实现,它确切地告诉你何时标头完全可见,你可以进一步细化逻辑..它只是一个快速的实现形式我。

    // Set a on touch listener for your list
    mList.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
           // get the top of  first child
            View mView = mList.getChildAt(0);
            int top = mView.getTop();


            switch(event.getAction()){

            case MotionEvent.ACTION_MOVE:
                // see if it top is at Zero, and first visible position is at 0
                if(top == 0 && mList.getFirstVisiblePosition() == 0){
                    Toast.makeText(MainActivity.this, "Header Item Visible", 
                            Toast.LENGTH_SHORT).show();
                }
            }
            // dont forget to retrun false here
            return false;
        }

    });

答案 1 :(得分:1)

您也可以在onScroll Listener中使用visibleitemCount来简单地标识它。

首先在列表视图的顶部(包括标题)标识列表视图的可见项目数。您可以通过敬酒或调试代码来获得计数。

就我而言 我的列表视图的常规visibleItemCount是3,在顶部它是2(带有headerview),那么我的代码对我有用。

{
    "_id" : ObjectId("1115dd31af82eb3ca8028188"),
    "data" : {
        "TL" : {
            "T001" : {
                "11" : "05012017",
                "13" : "0",
                "28" : "000",
                "29" : "00000",
                "30" : "01012017",
                "31" : "01122014",
                "36" : "10000",
                "37" : "3000",
                "38" : "29.81",
                "39" : "1",
                "44" : "03",
                "02" : "NOT DISCLOSED",
                "04" : "10",
                "05" : "1",
                "08" : "16122014"
            }
        }
    }
},
{
    "_id" : ObjectId("345222ddaf82eb1b262be44f"),
    "data" : {
        "TL" : {
            "T004" : {
                "10" : "19052013",
                "11" : "15062013",
                "12" : "37903",
                "13" : "0",
                "28" : "00000000",
                "29" : "000000000000000000",
                "30" : "01052013",
                "31" : "01062011",
                "44" : "03",
                "02" : "NOT DISCLOSED",
                "04" : "10",
                "05" : "1",
                "08" : "27062011",
                "09" : "08052013"
            },
            "T005" : {
                "11" : "10012017",
                "12" : "114525",
                "13" : "8853",
                "28" : "00000300000300000",
                "29" : "000000XXX0000000010",
                "30" : "01012017",
                "31" : "01022014",
                "36" : "100000",
                "37" : "10000",
                "44" : "03",
                "45" : "6714",
                "02" : "NOT DISCLOSED",
                "04" : "10",
                "05" : "1",
                "08" : "27062011",
                "09" : "12122016"
            },
        }
    }
}