在android系统Horizo​​ntalScrollView运动 [英] HorizontalScrollView motion in android

查看:185
本文介绍了在android系统Horizo​​ntalScrollView运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序中,有两个Horizo​​ntalScrollViews我使用。这是我的XML文件。

In my android application, there are two HorizontalScrollViews I am using. Here is my xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/hScroll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/Blue" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txtFriend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/txtFriend"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/txtList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/txtList"
                android:textColor="#000000"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/txtWork"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/txtWork"
                android:textColor="#000000"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/txtPlay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/txtPlay"
                android:textColor="#000000"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/txtBuddies"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/txtBuddies"
                android:textColor="#000000"
                android:textSize="17sp" />
        </LinearLayout>
    </HorizontalScrollView>

    <HorizontalScrollView
        android:id="@+id/hScroll2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ListView
                android:id="@+id/listFriend"
                android:layout_width="67dp"
                android:layout_height="280dp" />

            <ListView
                android:id="@+id/listList"
                android:layout_width="67dp"
                android:layout_height="280dp"
                android:layout_marginLeft="6dp" >
            </ListView>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

现在,我要做的就是当我移动第二水平滚动视图(这里ID = hScroll2)然后同时我的第一水平滚动视图必须移动。这是我的java code。

Now, what I have to do is when I move 2nd horizontal scrollview (here id =hScroll2) then Simultaneously my 1st horizontal scroll view must be moved. Here is my java code.

 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;

            }
        });

但是,当我水平从左侧移动第二滚动视图到右,第一个滚动视图与移动它在同一时间,但是当我提出第二次回到其位置(从右到左),那么第一个一不移动。它贴在新的位置。我在想什么?

But, when I am moving 2nd scroll view horizontally from Left to Right, 1st scroll view is moving with it at same time, but when I am moving 2nd back to its position(Right to Left), then 1st one is not moving. It sticks on new position. What am I missing?

推荐答案

OK,我带着一个小的解决方法这样的需求。我在我的项目之一实现了它,它工作正常:

OK, I come with a small workaround for such needs. I have implemented it in one of my projects and it works fine:

创建一个自定义Horizo​​ntalScrollView和ScrollListener接口得到的结果你想要的:

make a custom HorizontalScrollView and a ScrollListener interface to get the results You want:

    public class ExampleScrollView extends HorizontalScrollView {

private ScrollViewListener scrollViewListener = null;

/**
 * constructor
 * 
 * @param context
 *            current contex
 * @param attrs
 *            AttributeSet
 * @param defStyle
 *            style
 */
public ExampleScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // TODO Auto-generated constructor stub
}

/**
 * constructor
 * 
 * @param context
 *            current contex
 * @param attrs
 *            AttributeSet
 */
public ExampleScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);

}

/**
 * constructor
 * 
 * @param context
 *            current context
 */
public ExampleScrollView(Context context) {
    super(context);

}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    // TODO Auto-generated method stub

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

}

/**
 * sets the scrollViewListener
 * 
 * @param scrollViewListener
 */
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
    this.scrollViewListener = scrollViewListener;
}

 }

ScollViewListener接口

    public interface ScrollViewListener {

void onScrollChanged(ExampleScrollView scrollView, int x, int y, int oldx,
        int oldy);

    }

使用滚动型

    private ExampleScrollView mScrollViewOne;
    private ExampleScrollView mScrollViewTwo;

    mScrollOne = (ExampleScrollView) findViewById(R.id.horizontal_one);
    mScrollOne.setScrollViewListener(new ScrollViewListener() {

        @Override
        public void onScrollChanged(ExampleScrollView scrollView, int x,
                int y, int oldx, int oldy) {
            // TODO Auto-generated method stub
            mScrollTwo.scrollTo(x, y);
        }

    });

    mScrollTwo = (ExampleScrollView) findViewById(R.id.horizontal_two);
    mScrollTwo.setScrollViewListener(new ScrollViewListener() {

        @Override
        public void onScrollChanged(ExampleScrollView scrollView, int x,
                int y, int oldx, int oldy) {
            // TODO Auto-generated method stub
            mScrollOne.scrollTo(x, y);
            }

    });

在layout.xml

使用

     <com.yourpackage.ExampleScrollView
    android:id="@+id/horizontal_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    >

我希望这有助于有点...

I hope this helps a little bit...

这篇关于在android系统Horizo​​ntalScrollView运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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