15

I am trying to listen to the event when the HorizontalScrollView is scrolled. Tried this but it does not print anything.

HorizontalScrollView headerScrollView = new HorizontalScrollView(this);

    headerScrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Log.i("hv1",event.toString());
            Log.i("hv1","HELLO");
            return false;
        }
    });

The actual problem is, I want to scroll two HorizontalScrollView at a time..ie; both of them need to scroll simultaneously when atleast one of them scrolled. any workaround?

I used the answer below and then tried implementing this but I am not sure how I need to use the methods in the class.

TestHorizontalScrollView headerScrollView = (TestHorizontalScrollView) findViewById(R.id.headerHv); 

Is this the way I need to point to the hsv element in the layout file?

Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
i_raqz
  • 2,919
  • 10
  • 51
  • 87

6 Answers6

28

You may want to try creating your own custom class that extends HorizontalScrollView and overriding the onScrollChanged() function as such

public class TestHorizontalScrollView extends HorizontalScrollView {

    public TestHorizontalScrollView(Context context) {
        super(context);
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        // TODO Auto-generated method stub
        Log.i("Scrolling", "X from ["+oldl+"] to ["+l+"]");
        super.onScrollChanged(l, t, oldl, oldt);
    }

}

This overriden function will catch all changes to the scroll position even when the view is not being touched. This should keep your scroll views in sync.

David
  • 797
  • 7
  • 13
  • thanks for that. Well, i have two listviews which i have wrapped with a horizontal scroll view individually. My requirement is, when either one of the list is scrolled, the also has to scroll at the same time. Please share if you have any ideas regarding the same. Thanks. – i_raqz Jun 24 '11 at 03:17
  • So would this be something like the default news and weather app on Android? You can scroll left / right between ListViews, except with two ListViews in view? I'm wondering if you can just combine the two ListViews into one, and separating the two sides with LinearLayouts. Trying to visualize it :) – David Jun 25 '11 at 03:43
  • hmm..i think you dint get it right..let me explain again with a better example, I need to design a table kinda thing which looks very similar to excel spreadsheet..the column headings are stored in one list and the content below is stored in one list(this will have several rows)...when the list(content) below is scrolled, the header should also scroll along with that..i need to have two seperate lists (header and content) as this kinda table would be a generic table and would be built during runtime with different headers and content... – i_raqz Jun 27 '11 at 13:56
  • Also, i tried this, but it does not work..something seems to be wrong for sure..kindly guide TestHorizontalScrollView headerScrollView = (TestHorizontalScrollView) findViewById(R.id.headerHv); please guide me as to how I use this class – i_raqz Jun 27 '11 at 17:21
  • I believe I had done what was provided above before. I will have to check later tonight. I can edit my post once I verify the solution. :) – David Jun 28 '11 at 16:47
  • How can i Use this in my ArrayAdapter calss? – Manikanta Ottiprolu May 08 '15 at 05:34
  • @Manikanta this is a very very old post. You may want to ask a new question and reference this one. Also you'll probably want to say what you are trying to accomplish. If you ask about what you're trying to accomplish, (assuming a HorizontalListView or something) there may be a library or open source code you can use and you could get better help. – David May 13 '15 at 11:57
  • google engineers sat down one day, and said, let's create a horizontal scroll view and forgot to add a tracking event until api 23! just appalling. – TatiOverflow Oct 02 '18 at 19:43
8

old question, but maybe helpful. You can do something like this:

scrollOne = (HorizontalScrollView)findViewById(R.id.horizontal_one);
scrollTwo = (HorizontalScrollView)findViewById(R.id.horizontal_two);

scrollTwo.setOnTouchListener(new OnTouchListener(){

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub

        int scrollX = view.getScrollX();
        int scrollY = view.getScrollY();

        scrollOne.scrollTo(scrollX, scrollY);
                    return false;
        }

    });
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • 1
    Hey thank you for your answer. But when I am moving HorizontalScrollView again back to its original position, then first one is not moving with it...What am I missing? – Looking Forward Jul 20 '13 at 07:15
  • I Think the problem is, that You havent set both scroll views to the listener. Do this example above with both horizontal scroll views, the scrollOne in this example needs also an onTouchListener and inside, move scrollTwo. – Opiatefuchs Jul 20 '13 at 08:23
  • I am not getting your answer..I have updated my code here...can you tell me what am I missing – Looking Forward Jul 20 '13 at 08:26
  • And, surely what I forgot, If You scroll any scrollView programmatically, You have to do this code where You scroll it. This above example only works in onTouch – Opiatefuchs Jul 20 '13 at 08:26
  • hs2.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int scrollX = v.getScrollX(); int curX = scrollX; hs1.scrollBy(scrollX, 0); return false; } }); – Looking Forward Jul 20 '13 at 08:27
  • could You post more of Your code? I need to see every part where You are using both views – Opiatefuchs Jul 20 '13 at 08:40
  • I have updated my code here...please check this link http://stackoverflow.com/questions/17760191/horizontalscrollview-motion-in-android – Looking Forward Jul 20 '13 at 08:43
5

ScrollView with Listener, api < 23

write below in your code

MyHorizontalScrollView scrollView = (MyHorizontalScrollView)view.findViewById(R.id.scrollViewBrowse);
        scrollView.setOnScrollChangedListener(new MyHorizontalScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int l, int t, int oldl, int oldt) {

            }
        });

MyHorizontalScrollView

   public class MyHorizontalScrollView extends ScrollView {

        public OnScrollChangedListener mOnScrollChangedListener;

        public MyHorizontalScrollView(Context context) {
            super(context);
        }

        public MyHorizontalScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);

            if (mOnScrollChangedListener != null) {
                mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
            }
        }

        public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener){
            this.mOnScrollChangedListener = onScrollChangedListener;
        }

        public interface OnScrollChangedListener{
            void onScrollChanged(int l, int t, int oldl, int oldt);
        }

    }

* Xml file *

<MyHorizontalScrollView
        android:id="@+id/scrollViewBrowse"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/backgroung"
        android:padding="10dp">
 </MyHorizontalScrollView>
Fer
  • 1,956
  • 2
  • 28
  • 35
Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
2

To Avoid the the Api Level Problem... follow this.

scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            int scrollY = scrollView.getScrollY(); 
            int scrollX = scrollView.getScrollX();

            if (scrollY > 500) {

            } else {

            }
        }
    });
0

you can try this, i've tried so many ways, but none of them meet my needs, and then i tried to do it myself, and i succeed , and it seems good , it's smooth.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <com.szl.fundlistdemo.WrapContentListView
            android:id="@+id/left_lv"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:scrollbars="none"/>

        <HorizontalScrollView
            android:id="@+id/hscrollview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <com.szl.fundlistdemo.WrapContentListView
                android:id="@+id/right_lv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </HorizontalScrollView>
    </LinearLayout>


</ScrollView>

public class WrapContentListView extends ListView{
public WrapContentListView(Context context) {
    super(context);
}

public WrapContentListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public WrapContentListView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
}
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
陈志辉
  • 26
  • 2
0

Based @Dipak's answer In kotlin:

binding.scrollList.viewTreeObserver.addOnScrollChangedListener {
     val X = binding.scrollList.scrollX
     val Y = binding.scrollList.scrollY
}
William Hu
  • 15,423
  • 11
  • 100
  • 121