4

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.

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62
  • Your server request should not block the UI thread. How do you get the server response and feed your adapter? – Timuçin Feb 12 '14 at 10:19
  • @Tim It does not, I only mentioned it to emphasize that my items are dynamic. My question really lies on either my layout or missing `GridView` attribute which I'm not sure of. – Compaq LE2202x Feb 13 '14 at 01:06
  • Your GridView declaration seems ok. I myself tried an example with 2 items in the last row and there was no scrolling problem. Hard to find the reason without code. If you can share the code, I can help more. – Timuçin Feb 13 '14 at 08:57
  • @Tim I edited my post. The `GridView` is not really fullscreen but rather adjusts to what's left between the first 3 elements at the top and the last element at the bottom. – Compaq LE2202x Feb 13 '14 at 10:07
  • I tried the layout you described. Still didn't see any problems. Please post the xml of the full layout and the adapter code. There may be a problem in your getView method. – Timuçin Feb 13 '14 at 14:18
  • @Tim I just added the layouts and code. Thanks in advance for giving time. – Compaq LE2202x Feb 14 '14 at 09:56
  • I tried your code and still no problems. GridView scrolls with no problem. – Timuçin Feb 14 '14 at 10:35
  • @Tim This is so weird, I tried it in my dummy activity but it scrolls fine. I think it's in my actual items, instead of these `{ 1, 2, 3, 4, 5, 6, 7, 8 }`, I have an `ImageView` and a title in a `TextView` under it. These are being showed in my `item layout`. My mind is gonna blow! – Compaq LE2202x Feb 17 '14 at 01:04
  • Yes, it is weird. Send me your complete project and let me check it. ahmettimucin@gmail.com – Timuçin Feb 17 '14 at 06:54
  • That's the problem, I can't. It's proprietary, and I can't send also the exact images. – Compaq LE2202x Feb 17 '14 at 07:49
  • Hard to tell something helpful. I couldn't find any problems. You have to find it yourself. – Timuçin Feb 17 '14 at 07:57

0 Answers0