所有的ListView在ViewPager同步 [英] Synchronize all the ListView in a ViewPager

查看:91
本文介绍了所有的ListView在ViewPager同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序有一个ViewPager与每一个自定义的ListView它挂toghether像表中的行。问题是,当我滚动的ListView中的第一页,然后我滚动ViewPager第一页的ListView和第二页的ListView控件没有对齐。 ListView控件必须因为在第一次我有一个介绍一个TextView,然后4 coloumns与DATAS并在接下来的列表视图没有说明6 coloumns因此,如果他们没有对准它是非常复杂的对齐。所以,我怎么可以滚动所有的列表视图一次??

编辑:

非常感谢你为拉胡尔Parsani ...我终于设法如何做到这一点......如果你有ListView控件在不同的片段与片段的数量是不确定这就是答案:

 最后的ArrayList< ListView控件> LVS =新的ArrayList< ListView控件>();
        对于(INT J = 0; J< adapter.getCount(); J ++){
            YourFragment fragJ =(YourFragment)adapter.getItem(J);
            最终的ListView listViewJ = fragJ.lv;
            lvs.add(listViewJ);
        }

        对于(INT J = 0; J< lvs.size(); J ++){
            最终诠释X = D];
            lvs.get(J).setOnTouchListener(新OnTouchListener(){

                布尔派出= FALSE;
                @覆盖
                公共布尔onTouch(视图V,MotionEvent事件){
                    如果(!派遣){
                        派出= TRUE;
                        如果(X!= 0){
                            lvs.get(X  -  1).dispatchTouchEvent(事件);
                        };
                        如果(X = lvs.size() -  1){
                            lvs.get(X + 1).dispatchTouchEvent(事件);
                        }

                    }
                    派出= FALSE;
                    返回false;
                }
            });
        }
 

解决方案

你所要做的是同步列表视图。目前已经实现了这两个列表视图这里库。我会尽量解释的来源$ C ​​$与您位于在活动<一个C部分href="https://github.com/vladexologija/PinterestListView/blob/master/src/com/vladimir/pinterestlistview/ItemsActivity.java">here.

假设,如果您有通过变量访问所有的列表视图:

  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);
 

设置相同的触摸听众对他们的:

  listViewOne.setOnTouchListener(touchListener);
listViewTwo.setOnTouchListener(touchListener);
//同样,对于listViewThree和放大器; listViewFour
 

触摸监听器的基本思想是调度事件的其他的观点,使它们也同步地与列表视图被触摸滚动

  //传递触摸事件到对面的列表
OnTouchListener touchListener =新OnTouchListener(){
    布尔派出= FALSE;

    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        如果(v.equals(listViewOne)及&安培;!分派){
            派出= TRUE;
            listViewTwo.dispatchTouchEvent(事件);
            listViewThree.dispatchTouchEvent(事件);
            listViewFour.dispatchTouchEvent(事件);
        }否则如果(v.equals(listViewTwo)及&安培;!分派){
            派出= TRUE;
            listViewOne.dispatchTouchEvent(事件);
            listViewThree.dispatchTouchEvent(事件);
            listViewFour.dispatchTouchEvent(事件);
         } //同样,对于listViewThree和放大器; listViewFour
         派出= FALSE;
         返回false;
    }
};
 

图书馆还设置了滚动的倾听者,我相信是因为该观点可能不同高度,你没有的。尝试,让我知道如果这个工程。

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;
                }
            });
        }

解决方案

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.

这篇关于所有的ListView在ViewPager同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆