Android gridview stack from bottom

Viewed 623

I am trying to create a gridview consisting of 15x20 textviews and I want the textviews to be populated from bottom to top. e.g getChildAt(0) will refer to the grid at bottom left. Right now it is always referencing top left grid. I have tried

android:stackFromBottom="true"

but it isn't working. Below is my code, hope someone could give some insight what am i doing wrong. Thanks!

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

Using some of the solution you guys suggested, I created a function that correct my index for the grid. It will reference the grid from the last position.

EDIT :

   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;
   }
4 Answers

You don't need to fill the GridView in reverse order.. You just need to read it in reverse order.

Instead of:

gridView.getChildAt(position);

Use:

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

Of course, you should replace by the variable that you are using position - 1

android:stackFromBottom="true" doesn't mean your items will be placed in the opposite order.

It just means that if there is extra gap due to fewer items, space will be left in the top.

To place the items in opposite order, you will need to use override the getItem(int position) method to return item in reverse order in the adapter of the GridView.

To make android:stackFromBottom="true" work you must be using <GridView> instead of <GridLayout>.

I could be wrong but if you load in something like an array for the data in the grid view, then you can just reverse the array before loading it, or do what @W0rmH0lesaid above

Related