In my application I have a ViewPager with each one a custom ListView in it linked toghether like row in a table. The problem is that when I scroll a ListView in the first page and then I scroll the ViewPager the ListView of the first page and the ListView of the second page are misaligned. The listView must be aligned because in the first one i have a TextView with a description and then 4 coloumns with datas and in the next ListViews 6 coloumns without the description so if they are misaligned it's very complicated. So how can I scroll all the listViews at once ??
EDIT:
Thank you very much to Rahul Parsani... I've finally managed how to do this... If you have the listView in separate fragment and the number of the fragment is undefined this is the answer:
final ArrayList<ListView> lvs = new ArrayList<ListView>();
for (int j = 0; j < adapter.getCount(); j++) {
YourFragment fragJ = (YourFragment) adapter.getItem(j);
final ListView listViewJ = fragJ.lv;
lvs.add(listViewJ);
}
for (int j = 0; j < lvs.size(); j++) {
final int x = j;
lvs.get(j).setOnTouchListener(new OnTouchListener() {
boolean dispatched = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (!dispatched) {
dispatched = true;
if (x != 0) {
lvs.get(x - 1).dispatchTouchEvent(event);
};
if (x != lvs.size() - 1) {
lvs.get(x + 1).dispatchTouchEvent(event);
}
}
dispatched = false;
return false;
}
});
}
The problem you are having is similar to this one Update ViewPager dynamically?. It has nothing to do with setting
onTouchListener
because you don't have access to allListView
s insideViewPager
.Following is a working and tested code. Key point is
getItemPosition()
method inPagerAdapter
.What you are trying to do is synchronize the listviews. There is a library that already implements this for two listviews here. I will try to explain the part of the source code relevant to you which is located in the activity here.
Suppose if you have access to all your listviews through variables:
Set the same touch listeners on them:
The basic idea of the touch listener is to dispatch events to the other views so that they also scroll synchronously with the listview being touched.
The library also has set a scroll listener, which I believe is used because the views may be of differing heights, which you don't have. Try and let me know if this works.
Are the listview item heights equal? if so, you could pass the position of the top visible element to the other listview adapters.