简体   繁体   中英

GridView is not showing last row

I have the following GridView:

<GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="120dp"
        app:layout_constraintTop_toTopOf="parent"
        android:numColumns="2"
        android:gravity="center" />

and the following Item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center"
    >
    <LinearLayout
        android:orientation="vertical" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <TextView
            android:id="@+id/planlayout_teams_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            tools:text="Tambora 0 - 0 Balagath"
            android:textStyle="bold"
            android:paddingTop="4dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:gravity="center"/>

        <TextView
            android:id="@+id/planlayout_status_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            tools:text="noch unentschieden"
            android:gravity="center"/>
    </LinearLayout>
    <TextView
        android:id="@+id/planlayout_menu_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text=".\n.\n."
        android:textSize="12sp" />
</LinearLayout>

And everything is working fine except for the last item(s). Whether there is one or two items left, the whole row is not displayed. I googled a lot but nothing could help me.

Edit: I have the same "problem" as here: Custom GridView's Last row items not showing fully but those links don't help me.

I think the problem is about your layout. Your adapter load your all items but your layout not let you see last item because of your top margin. You can edit your grid layout like this. Can you try this way.

 <GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="120dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:numColumns="2"
    android:gravity="center" />

just try this -->

 <GridView
    android:id="@+id/mGridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:columnWidth="172dp"
    android:gravity="center"
    android:horizontalSpacing="5dp"
    android:numColumns="auto_fit"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp"/>

Or try with custom gridview

your gridview code on layout something like this :--

     <com.yourpackagename.MyGridView
        android:id="@+id/mGridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="2"
        android:adjustViewBounds="true"
        android:columnWidth="90dp"
        android:verticalSpacing="-2dp"
        android:horizontalSpacing="-10dp"
        android:stretchMode="columnWidth"
        android:scrollbars="vertical"
        android:fastScrollEnabled="true"
        />

MyGridview activity:-

public class MyGridView extends GridView {
public MyGridView(Context context) {
    super(context);
}

public MyGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec;

    if (getLayoutParams().height == LayoutParams.MATCH_PARENT) {

        heightSpec = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    }
    else {
        heightSpec = heightMeasureSpec;
    }

    super.onMeasure(widthMeasureSpec, heightSpec);
}
 }

In your mainactivity (activity where you want to declare the gridview)

    private MyGridView mGridView;
    //also add your adapter of gridview
    .
    .
    oncreate(){
     mGridView = getView().findViewById(R.id.mGridView);
     //then attach your adapter
      

    adapter = new GridViewAdapter(getContext()...whatever parameters you have in your constructor);
    mGridView.setAdapter(adapter);
     

try this custom gridview...still it doesnt worked let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM