4

当我收到不完整的行项目编号时,我遇到了问题GridView,它不再滚动。我的项目是由服务器提供的动态的,我有 3 列,如果GridView项目的数量足以启用scrolling并且如果行项目是 3,它工作正常。例如(9 项): 完全的

但如果第三行只有 1 或 2 个项目/秒,它不再滚动(7 或 8 个项目): 不完整

这让我头痛多于兴奋,我GridView的布局是:

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

在我的活动中没什么,只是声明和设置它的适配器。我错过了什么吗?我尝试单独添加verticalSpacing和,padding但没有任何改变。

编辑: 这是整个布局中的设置:

整体布局

Black是 a RelativeLayoutFrameLayout最后一个ImageView分别上下对齐。顶部TextView在下方FrameLayout,底部TextView在顶部下方TextView。现在GridView在 bottomTextView和 above ImageView,它应该根据前 3 个元素和底部元素之间剩余的空间来扩展它的高度。这就是它在我的布局中的设置方式。

我相信这是一个布局问题,而不是我的代码中的问题,因为我已经完成了几十个ListView并且这具有类似的实现。

EDIT2: 布局:

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

不要介意ImageViews,我只是取出了它们的来源。我需要从我的实际布局中保留设置。编码:

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

}

适配器:

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

项目布局:

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

尝试添加数字以gridViewItem添加足以触发滚动的项目,就像在我的屏幕截图中一样,并尝试在底部取出 1 或 2 个项目,这样该行将不完整以复制我的问题。谢谢。

4

0 回答 0