android - 如何在线性布局中一起滚动 2 个 GridView?

标签 android android-layout gridview scroll

我有两个线性布局的 GridView 。这是我的 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>

如何使这两个 GridView 一起滚动或同步?当我运行我的应用程序时,它们有两个单独的卷轴。

谢谢

最佳答案

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

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

现在,如果您的两个 View 不具有可比性,则这不会!按预期工作。内容数量相同。

关于android - 如何在线性布局中一起滚动 2 个 GridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11549613/

相关文章:

android - 结合 Activity 自定义动画和场景过渡动画

Android-将dp单位转换为像素单位,系数为0.5f

android - 如何将tablayout的指示器从底部更改为顶部?

html - GridView 对比HTML 表格 (ASP.net 2.0)

asp.net - 从我的 GridView 行中取回对象

java - 在 gridview 中添加 switch case 后应用程序崩溃

android - 在android中检测抖动

android - 如何使 GridLayoutManager 中的某个项目的高度相对于另一个项目进行调整?

android - 如何使用 robolectric 用额外的数据测试开始的 Intent

java - 如何在android中的图像上放置黑色透明