0

私の Android アプリケーションでは、使用している 2 つの Horizo​​ntalScrollViews があります。これが私のxmlファイルです。

<?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>

今、私がしなければならないことは、2 番目の水平スクロール ビュー (ここでは id =hScroll2) を移動するときです。同時に、最初の水平スクロール ビューを移動する必要があります。これが私のJavaコードです。

 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;

            }
        });

しかし、2番目のスクロールビューを左から右に水平に移動すると、1番目のスクロールビューも同時に移動しますが、2番目のスクロールビューをその位置(右から左)に戻すと、1番目のスクロールビューは移動しません。新しい位置にくっつきます。私は何が欠けていますか?

4

1 に答える 1

2

わかりました、そのようなニーズに対する小さな回避策があります。私は自分のプロジェクトの 1 つでそれを実装しましたが、正常に動作します。

カスタム Horizo​​ntalScrollView および ScrollListener インターフェイスを作成して、必要な結果を取得します。

ExampleScrollView.java

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

    }

ScrollView を使用する

    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"
    >

これが少し役立つことを願っています...

于 2013-07-20T09:49:24.853 に答える