прослушивание прокрутки событий horizontalscrollview android

Я пытаюсь прослушать событие, когда HorizontalScrollView прокручивается. Пробовал это, но ничего не печатает.

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

фактическая проблема в том, что я хочу прокрутить два HorizontalScrollView одновременно..ie; оба они должны прокручиваться одновременно, когда по крайней мере один из них прокручивается. есть обходной путь?

я использовал ответ ниже, а затем попытался реализовать это, но я не уверен, как мне нужно использовать методы в классе.

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

это то, что мне нужно указать на элемент hsv в файле макета?

4 ответов


вы можете попробовать создать свой собственный класс, который расширяет HorizontalScrollView и переопределяет функцию onScrollChanged () как таковую

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

}

эта переопределенная функция будет улавливать все изменения положения прокрутки, даже если вид не затрагивается. Это должно синхронизировать ваши представления прокрутки.


старый вопрос, но может быть полезно. Вы можете сделать что-то вроде этого:

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

    });

ScrollView с Прослушивателем, api

напишите ниже в своем коде

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-файл*

<MyHorizontalScrollView
        android:id="@+id/scrollViewBrowse"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/backgroung"
        android:padding="10dp">
 </MyHorizontalScrollView>

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