平滑滚动后如何更改列表项的背景?

时间:2013-10-11 16:06:30

标签: android android-listview smooth-scrolling

我在关闭列表项的视图时遇到问题。

首先,我顺利滚动它

//locationsList is a listview
locationsList.post(new Runnable() {
    @Override
    public void run() {
        locationsList.smoothScrollToPosition(k);
    }
});

然后我想做

View listItem = locationsList.getChildAt(k);
listItem.setBackgroundColor(getResources().getColor(R.color.highlight));

滚动完成后如何调用?

2 个答案:

答案 0 :(得分:0)

为listitem视图设置唯一标记。每当您想要更改滚动时,都可以使用此标记访问此列表项。

答案 1 :(得分:0)

如果您只想知道滚动完成的时间,则需要在列表视图中添加onScrollListener

locationsList.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState==OnScrollListener.SCROLL_STATE_IDLE){
                    View listItem = locationsList.getChildAt(k);
                    listItem.setBackgroundColor(getResources().getColor(R.color.highlight));
                }

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

当然上面只需要一个逻辑来更新背景,如果你之前已经开始滚动,那么你应该使用一个布尔标志并在locationsList.smoothScrollToPosition(k);执行时将其设置为true,然后如果这个标志是如果为true,请在scrollListener中运行代码并将标志设置为false