【问题标题】:GridView is not showing last rowGridView 不显示最后一行
【发布时间】:2020-09-06 19:38:23
【问题描述】:

我有以下 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" />

以及以下物品:

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

除了最后一项外,一切正常。无论剩下一项还是两项,都不会显示整行。我用谷歌搜索了很多,但没有任何帮助。

编辑:我有与这里相同的“问题”:Custom GridView's Last row items not showing fully 但这些链接对我没有帮助。

【问题讨论】:

  • 您是以编程方式将此项目添加到网格中还是什么?
  • 是的,使用适配器。
  • @TatianaGerth 尝试将您的高度更改为 gridview 的 fill_parent
  • 还是一样,并且仍然不推荐使用 fill_parent
  • 我对 RecyclerView 进行了同样的尝试,并且成功了!

标签: android android-layout gridview android-gridview


【解决方案1】:

我认为问题在于您的布局。您的适配器会加载您的所有项目,但您的布局不会让您看到最后一个项目,因为您的上边距。 您可以像这样编辑网格布局。你可以试试这个方法吗?

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

【讨论】:

  • 感谢您的回答,但这也不起作用。但是这个和我的解决方案之间的区别在于,当我向下滚动时,这个解决方案给人的感觉是我到了最后,当你想向上滚动更多时,这个动画就像在开始时一样。在我的解决方案中没有动画。我也试过没有上边距,问题仍然存在。
【解决方案2】:

试试这个 -->

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

或尝试使用自定义网格视图

您在布局上的 gridview 代码如下所示:--

     <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 活动:-

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);
}
 }

在您的 mainactivity(您要声明 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);
     

试试这个自定义的gridview...还是不行告诉我

【讨论】:

  • 谢谢,但也没有用。
  • @TatianaGerth 查看我的最新编辑 .. 仍然不起作用让我知道
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 2019-07-29
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 2015-05-04
相关资源
最近更新 更多