public void setListViewOnTouchAndScrollListener(final ListView listView1,
final ListView listView2) {
// 设置listview2列表的scroll监听,用于滑动过程中左右不同步时校正
listView2.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 如果停止滑动
if (scrollState == 0 || scrollState == 1) {
// 获得第一个子view
View subView = view.getChildAt(0);
if (subView != null) {
final int top = subView.getTop();
final int top1 = listView1.getChildAt(0).getTop();
final int position = view.getFirstVisiblePosition();
// 如果两个首个显示的子view高度不等
if (top != top1) {
listView1.setSelectionFromTop(position, top);
}
}
}
}
public void onScroll(AbsListView view, final int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
View subView = view.getChildAt(0);
if (subView != null) {
final int top = subView.getTop();
// //如果两个首个显示的子view高度不等
int top1 = listView1.getChildAt(0).getTop();
if (!(top1 - 7 < top && top < top1 + 7)) {
listView1.setSelectionFromTop(firstVisibleItem, top);
listView2.setSelectionFromTop(firstVisibleItem, top);
}
}
}
});
// 设置listview1列表的scroll监听,用于滑动过程中左右不同步时校正
listView1.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == 0 || scrollState == 1) {
// 获得第一个子view
View subView = view.getChildAt(0);
if (subView != null) {
final int top = subView.getTop();
final int top1 = listView2.getChildAt(0).getTop();
final int position = view.getFirstVisiblePosition();
// 如果两个首个显示的子view高度不等
if (top != top1) {
listView1.setSelectionFromTop(position, top);
listView2.setSelectionFromTop(position, top);
}
}
}
}
@Override
public void onScroll(AbsListView view, final int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
View subView = view.getChildAt(0);
if (subView != null) {
final int top = subView.getTop();
listView1.setSelectionFromTop(firstVisibleItem, top);
listView2.setSelectionFromTop(firstVisibleItem, top);
}
}
});
}