2017-10-06 70 views
1

我想创建一个由15x20 textviews组成的gridview,我希望textviews从下到上填充。例如getChildAt(0)将指向左下方的网格。现在它总是引用左上角的网格。我试过从底部的Android GridView堆栈

android:stackFromBottom="true" 

但它不工作。下面是我的代码,希望有人可以提供一些见解我做错了什么。谢谢!

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/sample_main_layout"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="313dp" 
    android:layout_weight="0.18"> 

    <GridLayout 
     android:id="@+id/map_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:columnCount="15" 
     android:descendantFocusability="afterDescendants" 
     android:gravity="center" 
     android:horizontalSpacing="30dp" 
     android:paddingLeft="20dp" 
     android:rowCount="20" 
     android:stackFromBottom="true" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="30dp"> 

    </GridLayout> 

使用你们建议的一些解决方案,我创建了一个函数来纠正我的网格索引。它将引用最后一个位置的网格。

编辑:

public int correctIndex(int index) { 
    int mul = (int) Math.floor(index /15); 
    int remainder = index%15; 
    remainder = 15-remainder; 
    int correctedIndex = 300-(mul*15+remainder); 
    return correctedIndex; 
    } 

回答

1

您不需要按相反顺序填入GridView ..您只需要按相反顺序阅读它。

相反的:

gridView.getChildAt(position); 

用途:

gridView.getChildAt(gridView.getChildCount() - position); 

当然,你应该通过变量替换正在使用

+0

谢谢!这工作!但是我已经加入了一些计算来完成相反的操作! – Jackelll

1

android:stackFromBottom="true"并不意味着你的项目将被放置在相反的顺序。

这只是表示如果由于较少的项目而存在额外的差距,空间将留在最上面。

要放置在相反的顺序中的项目,你将需要使用覆盖getItem(int position)方法以相反的顺序在GridView的适配器返回项目。

1

为了使android:stackFromBottom="true"工作,你必须使用<GridView>代替<GridLayout>

1

我可能是错的,但如果你的东西一样加载在网格视图中的数据数组,那么你可以扭转阵列加载它之前,还是做什么@ W0rmH0lesaid上述