I’m trying to scroll 2 listviews simultaneously using the setSelectionFromTop() method from the ListView API. Here’s my layout:
<?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>
and my Activity:
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
}
});
}
Everything works fine and the scrolling is smooth. However, when I wrap list_1 in the xml file in a LinearLayout, the synchronized scrolling is broken. Is this a bug or am I forgetting to do something?
Note: The data sourcing the ListViews is taken from the ListView tutorial on the Android developer’s site and both ListViews are using instances of the same Adapter. Assume that I implemented the usage patterns for the Adapters correctly. Also, I’ve tried using RelativeLayout and even hard-coding the pixel values in. I still get the same issue.
I resolved this issue by re-factoring the layout and putting both listviews at the same level on in the view hierarchy.