Synchronize all the ListView in a ViewPager(同步 ViewPager 中的所有 ListView)
问题描述
在我的应用程序中,我有一个 ViewPager,其中每个都有一个自定义 ListView,它像表格中的行一样链接在一起.问题是,当我在第一页滚动 ListView 然后滚动 ViewPager 时,第一页的 ListView 和第二页的 ListView 未对齐.listView 必须对齐,因为在第一个中我有一个带有描述的 TextView,然后是 4 个带有数据的列,而在接下来的 ListViews 中,有 6 个没有描述的列,所以如果它们未对齐,那将非常复杂.那么如何一次滚动所有的 listView 呢??
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 ??
非常感谢 Rahul Parsani...我终于设法做到了...如果您在单独的片段中有 listView 并且片段的数量未定义,这就是答案:
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;
}
});
}
推荐答案
您要做的是同步列表视图.here 有一个库已经为两个列表视图实现了此功能.我将尝试解释位于活动中与您相关的源代码部分 这里.
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:
listViewOne = (ListView) findViewById(R.id.list_view_one);
listViewTwo = (ListView) findViewById(R.id.list_view_two);
listViewThree = (ListView) findViewById(R.id.list_view_three);
listViewFour = (ListView) findViewById(R.id.list_view_four);
在它们上设置相同的触摸监听器:
Set the same touch listeners on them:
listViewOne.setOnTouchListener(touchListener);
listViewTwo.setOnTouchListener(touchListener);
// similarly for listViewThree & listViewFour
触摸监听器的基本思想是将事件分派给其他视图,以便它们也与被触摸的列表视图同步滚动.
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.
// Passing the touch event to the opposite list
OnTouchListener touchListener = new OnTouchListener() {
boolean dispatched = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.equals(listViewOne) && !dispatched) {
dispatched = true;
listViewTwo.dispatchTouchEvent(event);
listViewThree.dispatchTouchEvent(event);
listViewFour.dispatchTouchEvent(event);
} else if (v.equals(listViewTwo) && !dispatched) {
dispatched = true;
listViewOne.dispatchTouchEvent(event);
listViewThree.dispatchTouchEvent(event);
listViewFour.dispatchTouchEvent(event);
} // similarly for listViewThree & listViewFour
dispatched = false;
return false;
}
};
该库还设置了一个滚动侦听器,我相信它已被使用,因为视图可能具有不同的高度,而您没有.尝试让我知道这是否有效.
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.
这篇关于同步 ViewPager 中的所有 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:同步 ViewPager 中的所有 ListView
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01