3

我有两个ListViewsListViews当我滚动任何一个列表时,有什么方法可以同步位置。我正在实施一个AbsListView.OnScrollListener,注册到ListView. 当ListView滚动时,将触发的onScroll()方法OnScrollListener,然后我调用'smoothScrollToPosition()'。但它不能正常工作。有人可以为此提供任何代码示例吗?

4

2 回答 2

5

这是 nininho 建议的工作代码示例

主要活动

public class MainActivity extends Activity {

public static String[] Cheeses = new String[] { "Abbaye de Belloc", "Abbaye du Mont des Cats",
        "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
        "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Alverca",
        "Ambert", "American Cheese", };

ListView list1;
ListView list2;

boolean isLeftListEnabled = true;
boolean isRightListEnabled = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            Cheeses);

    list1 = (ListView) findViewById(R.id.list1);
    list1.setAdapter(adapter);

    list2 = (ListView) findViewById(R.id.list2);
    list2.setAdapter(adapter);

    // IF YOU DO NOT OVERRIDE THIS
    // ONLY THE ONE THAT IS TOUCHED WILL SCROLL OVER
    list1.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    list2.setOverScrollMode(ListView.OVER_SCROLL_NEVER);

    list1.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // onScroll will be called and there will be an infinite loop.
            // That's why i set a boolean value
            if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                isRightListEnabled = false;
            } else if (scrollState == SCROLL_STATE_IDLE) {
                isRightListEnabled = true;
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
            View c = view.getChildAt(0);
            if (c != null && isLeftListEnabled) {
                list2.setSelectionFromTop(firstVisibleItem, c.getTop());
            }
        }
    });

    list2.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                isLeftListEnabled = false;
            } else if (scrollState == SCROLL_STATE_IDLE) {
                isLeftListEnabled = true;
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
            View c = view.getChildAt(0);
            if (c != null && isRightListEnabled) {
                list1.setSelectionFromTop(firstVisibleItem, c.getTop());
            }
        }
    });
}
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:splitMotionEvents="true">
    <ListView android:id="@+id/list1"
              android:layout_width="0dip"
              android:layout_height="match_parent"
              android:layout_weight="1" />
    <ListView android:id="@+id/list2"
              android:layout_width="0dip"
              android:layout_height="match_parent"
              android:layout_weight="1" />
</LinearLayout>

于 2013-04-23T11:56:25.073 回答
1

如果位置可见,smoothScrollToPosition() 将不会滚动。您可以考虑在当前未滚动的另一个列表上使用 scrollTo 或 scrollBy,但请注意不要输入递归,每个列表都调用另一个列表来滚动。

于 2012-08-10T17:57:07.070 回答