2

我在线性布局中有两个网格视图。这是我的 XML 代码:

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

   <GridView
    android:id="@+id/leftGrid2"
    android:layout_width="416dp"
    android:layout_height="1078dp"
    android:columnWidth="70dp"
    android:horizontalSpacing="5dp"
    android:numColumns="2"
    android:paddingRight="20dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp" >

  </GridView>

   <GridView
    android:id="@+id/rightGrid2"
    android:layout_width="400dp"
    android:layout_height="match_parent"
    android:columnWidth="70dp"
    android:horizontalSpacing="5dp"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp" >

    </GridView>
  </LinearLayout>

如何让这两个网格视图一起滚动或同步?当我运行我的应用程序时,它们有两个单独的滚动条。

谢谢,

4

2 回答 2

2

GridView通过其 AbsLisView 父级提供了smoothScrollToPosition(int)方法。这将滚动到您想要的位置。现在,您使用OnScrollListener监听 ScrollEvent 。现在只需将它们绑在一起:

GridView other;
@Override public void onScroll(AbsListView view, int firstItem, int visItems, int total) {
    other.smoothScrollToPosition(firstItem);
}

现在,这不会!如果您的两种观点不可比,即按预期工作。相同的内容量。

于 2012-07-18T23:07:21.017 回答
0

我以前必须用画廊做一次。你应该能够GridView很容易地把它应用到:

public class DualGallery extends Gallery {

/**
 * Second {@link Gallery}
 */
public DualGallery b;

/**
 * Current {@link Context}
 */
Context mContext;

/**
 * Constructor that takes the {@link Context} and second {@link DualGallery}
 * 
 * @param context
 * @param b
 */
public DualGallery(Context context, DualGallery b) {
    super(context);

    this.mContext = context;

    this.b = b;

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    // Scrolls this and the second Gallery avoiding an endless loop
    return super.onFling(e1, e2, velocityX, velocityY)
            && b.onFling2(e1, e2, velocityX, velocityY);
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    // Scrolls this and the second Gallery avoiding an endless loop
    return super.onScroll(e1, e2, distanceX, distanceY)
            && b.onScroll2(e1, e2, distanceX, distanceY);
}

@Override
public void setOnClickListener(OnClickListener l) {
    super.setOnClickListener(l);
}

@Override
public void setAdapter(SpinnerAdapter adapter) {
    super.setAdapter(adapter);
}

/**
 * Special function to redirect to just
 * {@link Gallery#onFling(MotionEvent, MotionEvent, float, float)}
 * 
 * @param e1
 * @param e2
 * @param velocityX
 * @param velocityY
 * @return
 */
public boolean onFling2(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    return super.onFling(e1, e2, velocityX, velocityY);
}

/**
 * Special function to redirect to just
 * {@link Gallery#onScroll(MotionEvent, MotionEvent, float, float)}
 * 
 * @param e1
 * @param e2
 * @param distanceX
 * @param distanceY
 * @return
 */
public boolean onScroll2(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return super.onScroll(e1, e2, distanceX, distanceY);
}

}

于 2012-07-18T23:21:38.413 回答