Synchronizing two Horizontal Scroll Views in Android

前端 未结 2 743
醉话见心
醉话见心 2021-01-14 02:16

I tried following the advice from this post Synchronise ScrollView scroll positions - android but running into a problem.

Background - I want a table layout with bo

相关标签:
2条回答
  • 2021-01-14 02:44

    This is my solution further explanation in the code:

    //This should be inside yout oncreate
    syncScrolls(firstView, secondView;
            syncScrolls(secondView, firstView);
    
    
    
    
        //This method accept 2 horizontal scroll views, but you could improve it passing an array. 
            private void syncScrolls(final HorizontalScrollView currentView, final HorizontalScrollView otherView) {
    
    //This create the Gesture Listener where we override the methods we need
                    GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
                        @Override
                        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    //Here onfling just return true, this way doesnt happens
                            return true;
                        }
                        @Override
                        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    //On scroll we sync the movement of the both views
                            otherView.scrollTo(currentView.getScrollX(), 0);
                            return super.onScroll(e1, e2, distanceX, distanceY);
                        }
                        @Override
                        public boolean onDown(MotionEvent e) {
                            return true;
                        }
                    };
            //This create the gesture detectors that implements the previous custom gesture listener
                    final GestureDetector gestureDetector = new GestureDetector(this, gestureListener);
            //And finally we set everything to the views we need
                    currentView.setOnTouchListener(new View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {
                            return gestureDetector.onTouchEvent(event);
                        }
                    });
    
            }
    

    Im not sure this is a completely good answers, well as I mentioned before you can improve this by using an array, insted of calling the method for each view to sync, int his case just 2. Also onFling is prevented instead of being sync, if your scroll view is too long, this could not be user friendly. And finally, there is still a litle problem if the user does a tiny fling, but its a very rare case.

    0 讨论(0)
  • 2021-01-14 02:53

    The error points to a null pointer in a constructor in your ObservableScrollView. Could you please post the constructor, as well as line 12 on its own?

    One possible issue I am seeing with the way your XML and custom scroll view interact is that your custom scroll view is an inner class. See this page for how to declare an inner class custom component in XML, or move it to an external class. I usually put it in an external class, but if you wanted to keep it as an inner class it would be something like

    <view
        class="com.glen.apps.TeacherAidePro$ObservableScrollView"
    .../>
    

    I don't think this explains your null pointer exception though, so please post your ObservableScrollView class.

    EDIT:

    If you insist on doing everything in Java, here is a working example:

    private ObservableScrollView scrollView1 = null;
    private ObservableScrollView scrollView2 = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        LinearLayout parent = new LinearLayout(this);
        parent.setOrientation(LinearLayout.HORIZONTAL);
        parent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        parent.setWeightSum(2.0f);
    
        scrollView1 = new ObservableScrollView(this);
        scrollView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT, 1.0f));
        scrollView2 = new ObservableScrollView(this);
        scrollView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT, 1.0f));
    
        scrollView1.setScrollViewListener(new ScrollViewListener() {
            public void onScrollChanged(ObservableScrollView scrollView, int x,
                    int y, int oldx, int oldy) {
                scrollView2.scrollTo(x, y);
            }
        });
        scrollView2.setScrollViewListener(new ScrollViewListener() {
            public void onScrollChanged(ObservableScrollView scrollView, int x,
                    int y, int oldx, int oldy) {
                scrollView1.scrollTo(x, y);
            }
        });
    
        TextView tv1 = new TextView(this);
        tv1.setText("TEXT1TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
        tv1.setTextSize(36.0f);
        scrollView1.addView(tv1);
    
        TextView tv2 = new TextView(this);
        tv2.setText("TEXT2TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
        tv2.setTextSize(36.0f);
        scrollView2.addView(tv2);
    
        parent.addView(scrollView1);
        parent.addView(scrollView2);
        parent.invalidate();
    
        setContentView(parent);
    }
    

    Basically, this creates two scroll views side by side, with weights of 1.0 each, and puts them in a LinearLayout with total weight on the layout of 2.0, so they each get half the width.

    However, I highly recommend getting used to XML as it is much, much easier (in my opinion) to create layouts. It is also easier to spot mistakes, and the nested form of XML makes it easier to read. Anyway, hope this clears things up.

    0 讨论(0)
提交回复
热议问题