本文介绍了如何syncronisize 2列表视图位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表视图。有没有什么办法,当我滚动列表中的任何一个同步列表视图的位置。林实施的 AbsListView.OnScrollListener ,注册到的ListView 。当的ListView 滚动时的 onScroll()方法 OnScrollListener 将被触发,然后我叫'smoothScrollToPosition()。但它doent正常工作。有人可以提供我任何code例子吗?

I have two ListViews. Is there any way to synchronize the position of ListViews when I scroll any one of the Lists. Im implementing an AbsListView.OnScrollListener, registering to the ListView. When the ListView is scrolled, the onScroll() method of OnScrollListener will be triggered, then i call `smoothScrollToPosition()'. But it doent work properly. Can someone provide me any code example for this?

推荐答案

下面是一个工作code什么nininho是建议样品

Here is a working code sample of what nininho is suggesting

MainActivity

MainActivity

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>

这篇关于如何syncronisize 2列表视图位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:11