Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android多个ListView和setSelectionFromTop()错误?_Android_Listview_Layout_Scroll - Fatal编程技术网

Android多个ListView和setSelectionFromTop()错误?

Android多个ListView和setSelectionFromTop()错误?,android,listview,layout,scroll,Android,Listview,Layout,Scroll,我试图使用ListView API中的setSelectionFromTop()方法同时滚动2个ListView。以下是我的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="

我试图使用ListView API中的setSelectionFromTop()方法同时滚动2个ListView。以下是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView
    android:id="@+id/list_1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5"/>
    <ListView
    android:id="@+id/list_2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5"/>
</LinearLayout>
一切正常,滚动顺畅。但是,当我将xml文件中的
list_1
包装成线性布局时,同步的滚动被破坏。这是一个错误还是我忘记做什么了


注意:ListView的数据来源取自Android开发者网站上的ListView教程,两个ListView都使用同一适配器的实例。假设我正确地实现了适配器的使用模式。此外,我还尝试使用RelativeLayout,甚至对中的像素值进行硬编码。我仍然会遇到同样的问题。

我通过重新分解布局并将两个ListView放在视图层次结构中的同一级别解决了这个问题。

实际上,我可能会被迫使用一个布局,该布局在层次结构树中不会同时包含两个ListView。我不明白的是,当列表_1包装在另一个视图中时,为什么同步滚动会被破坏

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ListView listView1 = (ListView)findViewById(R.id.list_1);
    listView1.setAdapter(new ListAdapter());

    ListView listView2 = (ListView)findViewById(R.id.list_2);
    listView2.setAdapter(new ListAdapter());
    listView2.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            View v = view.getChildAt(0);
            final int top = (v == null) ? 0 : v.getTop();

            listView1.setSelectionFromTop(firstVisibleItem, top);
        }

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

        }

    });
}