我在线性布局中有两个网格视图。这是我的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>
如何使这两个网格视图一起滚动或同步?当我运行我的应用程序时,它们有两个单独的卷轴。
谢谢,
答案 0 :(得分:2)
GridView通过其AbsLisView父级提供smoothScrollToPosition(int)方法。这将滚动到您想要的位置。现在,您使用OnScrollListener收听ScrollEvent。现在把这些结合在一起:
GridView other;
@Override public void onScroll(AbsListView view, int firstItem, int visItems, int total) {
other.smoothScrollToPosition(firstItem);
}
现在,如果你的两个观点不具有可比性,那么这将 not!按预期工作。相同的内容数量。
答案 1 :(得分:0)
在使用Gallery之前我必须这样做一次。您应该能够轻松地将其应用到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);
}
}