一起滚动ListViews

com*_*uru 6 java android

我有两个ListView想要滚动的对象.它们是并排的,所以如果一个滚动一定量,另一个滚动相同数量.我已经找到了一些关于如何做到这一点的例子,但我相信它们依赖于ListView相同高度的物品(如果我错了,请纠正我).我的一个物品中的物品ListView比另一物品中的物品高,跨越2-3个物品.

如何将这两个ListView物体"锁定" 在一起?

编辑:这是我所拥有的截图,也许它会更好地解释我的目标.左侧(红色)是项目列表,右侧是单独的列表.您可以看到列表如何不完美对齐,因此它不完全是网格.我想做的是让这个行为像一个大的列表,滚动任何一个列表也会滚动另一个.

应用截图

com*_*uru 6

我创建了一个粗略的课程,基本上完成了我想做的事情.如果第二个列表比第一个列表长或者方向改变,那么处理它并不够智能,但它足以让概念失效.

设置:

list1.setOnScrollListener(new SyncedScrollListener(list2));
list2.setOnScrollListener(new SyncedScrollListener(list1));
Run Code Online (Sandbox Code Playgroud)

SyncedScrollListener.java

package com.xorbix.util;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

public class SyncedScrollListener implements OnScrollListener{
    int offset;
    int oldVisibleItem = -1;
    int currentHeight;
    int prevHeight;
    private View mSyncedView;


    public SyncedScrollListener(View syncedView){

        if(syncedView == null){
            throw new IllegalArgumentException("syncedView is null");
        }

        mSyncedView = syncedView;
    }

    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {

        int[] location = new int[2];

        if(visibleItemCount == 0){
            return;
        }

        if(oldVisibleItem != firstVisibleItem){

            if(oldVisibleItem < firstVisibleItem){
                prevHeight = currentHeight;
                currentHeight = view.getChildAt(0).getHeight();

                offset += prevHeight;

            }else{
                currentHeight = view.getChildAt(0).getHeight();

                View prevView;
                if((prevView = view.getChildAt(firstVisibleItem - 1)) != null){
                    prevHeight = prevView.getHeight();
                }else{
                    prevHeight = 0;
                }

                offset -= currentHeight;
            }

            oldVisibleItem = firstVisibleItem;
        }

        view.getLocationOnScreen(location);
        int listContainerPosition = location[1];

        view.getChildAt(0).getLocationOnScreen(location);
        int currentLocation = location[1];

        int blah = listContainerPosition - currentLocation + offset;

        mSyncedView.scrollTo(0, blah);

    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

    }
}
Run Code Online (Sandbox Code Playgroud)