滚动ListView同时滚动视图

时间:2015-04-03 15:35:48

标签: android android-listview android-scrollview

我有一个带有ListView和自定义视图的RelativeLayout。我以编程方式将自定义视图的大小设置为ListView的大小。所以我在ListView的顶部有自定义视图,大小相同。显然ListView不滚动,因为onTouchListener在customView上。如何同时滚动?

1 个答案:

答案 0 :(得分:0)

最后,我在自定义视图类中创建了一个函数,其中ListView和行数作为参数。在视图中我将setOnScrollListener设置为ListView,并在滚动到自定义视图的全局滚动变量时指定ListView的位置。喜欢这个

public void setListView(final TwoWayView listView, int totalListRows) {
    totalColumns = totalListRows;
    for (int i = 0; i < totalColumns; i++) {
        listViewItemHeights.put(i,  (int) getResources().getDimension(R.dimen.column_width));
    }
    listView.setOnScrollListener(new TwoWayView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(TwoWayView view, int scrollState) {

        }

        @Override
        public void onScroll(TwoWayView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            c = listView.getChildAt(0);
            if (c != null) {
                int oldScrollX = scrollX;
                scrollX = -c.getLeft();


                for (int i = 0; i < listView.getFirstVisiblePosition(); ++i) {
                    if (listViewItemHeights.get(i) != null) {
                        scrollX += listViewItemHeights.get(i);
                    } 

                }
                scrollBy(scrollX - oldScrollX, 0);
            } 

        }
    });


}