GridView with incomplete row item does not scroll

1.1k Views Asked by At

I have a problem with my GridView when it receives incomplete row item number, it doesn't scroll anymore. My items are dynamic which are given by the server, I have 3 columns and if the number of GridView items are more than enough to enable scrolling and if the row item is 3, it works fine. For example (9 items): complete

But if the third row has only 1 or 2 item/s, it doesn't scroll anymore (7 or 8 items): incomplete

This has caused me headache more than excitement, my GridView in layout is:

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@color/white"
    android:listSelector="@android:color/transparent"
    android:numColumns="3"
    android:stretchMode="columnWidth" >
</GridView>

In my activity nothing much, just declaration and setting its adapter. Did I miss anything? I tried adding verticalSpacing and padding separately but nothing's changed.

EDIT: This is the setup in the whole layout:

whole_layout

Black is a RelativeLayout, FrameLayout and the last ImageView are aligned top and bottom respectively. Top TextView is below FrameLayout, bottom TextView is below top TextView. Now GridView is below bottom TextView and above ImageView, it should expand its height basing to the space left consumed between the first 3 elements and the bottom element. This is how it's setup in my layout.

I believe it's a layout problem more than in my code since I've done dozens of ListView and this has similar implementation.

EDIT2: The layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SixthActivity" >

    <FrameLayout
        android:id="@+id/cover_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/cover_image"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:background="@color/gray"
                android:scaleType="centerCrop" />

            <ProgressBar
                android:id="@+id/cover_image_progressbar"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="180dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center_vertical"
                    android:layout_margin="10dp" />

                <TextView
                    android:id="@+id/school_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp"
                    android:ellipsize="end"
                    android:maxLines="2"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/container_sns"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_margin="10dp"
                android:gravity="right"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/btn_telephone"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/btn_email"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/btn_facebook"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/btn_twitter"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/btn_line"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:visibility="gone" />
            </LinearLayout>
        </RelativeLayout>
    </FrameLayout>

    <TextView
        android:id="@+id/student_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/cover_frame"
        android:background="#f8f7f3"
        android:padding="10dp"
        android:textColor="@color/brown"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/container_news"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/student_name"
        android:background="@drawable/bg_news"
        android:clickable="true"
        android:orientation="horizontal"
        android:visibility="gone" >

        <TextView
            android:id="@+id/news"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="85dp"
            android:layout_marginRight="10dp"
            android:clickable="true"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="@color/brown"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/container_next_plan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/container_news"
        android:background="@drawable/bg_next"
        android:clickable="true"
        android:orientation="horizontal"
        android:visibility="gone" >

        <TextView
            android:id="@+id/next_plan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="85dp"
            android:layout_marginRight="10dp"
            android:clickable="true"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="@color/brown"
            android:textStyle="bold" />
    </RelativeLayout>

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/mypage_footer"
        android:layout_below="@id/container_next_plan"
        android:background="#ACA899"
        android:gravity="center"
        android:numColumns="3" >
    </GridView>

    <ImageView
        android:id="@+id/mypage_footer"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:scaleType="fitXY" />

</RelativeLayout>

Don't mind the ImageViews, I just took out their source. I needed to retain the setup from my actual layout. The code:

public class SixthActivity extends Activity {

    private Integer[] gridViewItem = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sixth);

        GridView gridView = (GridView) findViewById(R.id.grid_view);
        CustomAdapter adapter = new CustomAdapter(this,
                R.layout.item_module, gridViewItem);

        gridView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sixth, menu);
        return true;
    }

}

The adapter:

public class CustomAdapter extends ArrayAdapter<Integer> {

    private Context context;
    private int resource;
    private Integer[] moduleList;

    public CustomAdapter(Context context, int resource,
            Integer[] moduleList) {
        super(context, resource, moduleList);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.resource = resource;
        this.moduleList = moduleList;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);
            holder = new ViewHolder();
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        holder.tvModuleName = (TextView) row.findViewById(R.id.module_name);
        holder.tvModuleImage = (ImageView) row.findViewById(R.id.module_image);

        holder.tvModuleName.setText("SAMPLESAMPLE");
        return row;
    }

    public class ViewHolder {
        public ImageView tvModuleImage;
        public TextView tvModuleName;
    }
}

The item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_module"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/module_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/module_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ellipsize="end"
        android:singleLine="true" />

</LinearLayout>

Try to add numbers to gridViewItem to add items enough to trigger the scrolling just like in my screenshots, and try taking out 1 or 2 items at the bottom such that that row would be incomplete to replicate my problem. Thanks.

0

There are 0 best solutions below