GridView布局RowSpan问题

时间:2017-11-05 21:32:33

标签: java android xml android-layout gridview

我正在使用Android Studio构建一个Android应用程序,我希望在行中显示我的项目(使用imageViews的图像)(每个中有4个),但是当我添加超过4个项目时,而不是下一个项目被采取到下一行,它根本就没有显示出来。我错过了什么?我试过调整多个东西,但没有一个工作。请记住,我是Android应用程序开发的新手,所以我不确定我的布局选择是否“最佳”。您将找到我的xml文件来处理所有布局部分:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:padding="20dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="70dp"
                android:text="Cards List"
                android:textColor="@android:color/holo_blue_light"
                android:textSize="40sp" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <android.support.v7.widget.LinearLayoutCompat
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:verticalSpacing="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <GridView
                    android:id="@+id/gridView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:columnWidth="90dp"
                    android:gravity="center"
                    android:horizontalSpacing="10dp"
                    android:numColumns="4"
                    android:paddingBottom="500dp"
                    android:stretchMode="columnWidth"
                    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
                    android:verticalSpacing="10dp">

                </GridView>
            </LinearLayout>
        </android.support.v7.widget.LinearLayoutCompat>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

MAINACTIVITY.JAVA

package com.example.steli.hellogridview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    GridView gridView = (GridView) findViewById(R.id.gridView);
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Toast.makeText(MainActivity.this,"" + position,Toast.LENGTH_SHORT).show();
        }
    });
}
}

IMAGEADAPTER.JAVA

package com.example.steli.hellogridview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;

/ **  * HelloGridView是  *由Stelios Papamichail于11/5/2017创建。  *

 *此文件属于com.example.steli.hellogridview包。  * /

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// Create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if(convertView == null) {
        // If it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(180,180));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8,8,8,8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.ssj4_gogeta,
        R.drawable.cell_jr_n,
        R.drawable.mrsatan_n,
        R.drawable.tien,
        R.drawable.vegito,
        R.drawable.videl
};
}

1 个答案:

答案 0 :(得分:0)

您的Java代码没有任何问题。但是,你的XML非常混乱。

例如,为什么要在LinearLayout内的LinearLayoutCompat内级联NestedScrollView?要滚动GridView,您不需要任何。所以把它拿出来。

另外,android:paddingBottom="500dp"太多了。此行将GridView的一部分推到屏幕顶部之外(至少在我使用的模拟器上)。所以摆脱它(或使填充更小)。

我不明白你对AppBarLayout的要求,所以我保持不变。

由于您可能不希望GridView在其上呈现,我将AppBarLayoutGridView放在RelativeLayoutandroid:layout_below="@id/appbar" 1}}在GridView的XML中。

所以XML现在看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@+id/toolbar">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="70dp"
                    android:text="Cards List"
                    android:textColor="@android:color/holo_blue_light"
                    android:textSize="40sp" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appbar"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="4"
            android:stretchMode="columnWidth"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:verticalSpacing="10dp">

        </GridView>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>