【问题标题】:Android: how to determine the number of items in a row in GridViewAndroid:如何确定GridView中一行的项目数
【发布时间】:2014-08-26 08:45:55
【问题描述】:

我通过 GridView 绘制一个矩阵。在 4x4 的情况下,使用默认值可以正常工作(每行包含 4 个项目)。绘制 5x5 时会出现问题,每行只有 4 个项目。

我的想法:调整项目宽度 =(屏幕宽度)/每行项目数。没关系?如果它是正确的,有人可以给我代码来做到这一点(对不起,我是 Android 新手)

或者,谁能在所有情况下(5x5、6x6 等)给我其他想法或代码示例?

--- 我的代码 --- MainLayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/container" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" 
android:orientation="vertical" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    android:layout_margin="0dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"    
    android:gravity="center" >

</GridView>

图像适配器

public class ImageAdapter extends BaseAdapter {
private Context mContext;

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

@Override
public int getCount() {
    // TODO Auto-generated method stub
    //return 0;
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@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(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convertView;
    }

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

// references to our images
private Integer[] mThumbIds = {
        //green code: BBE833
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell, R.drawable.green_cell,
        R.drawable.green_cell
};

MyActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    SecureRandom securerandom = new SecureRandom();
    int randomLoop = 15;

    final GridView gridview = (GridView)findViewById(R.id.gridView1);
    final ImageAdapter imageAdapter = new ImageAdapter(this);
    for (int i = 0; i < randomLoop; i++) {
        int position = securerandom.nextInt(25);
        imageAdapter.updateCell(position);
    }
    gridview.setAdapter(imageAdapter);
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            //Toast.makeText(QuizActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            imageAdapter.updateCell(position);
            //a = 11;
            gridview.setAdapter(imageAdapter);
        }
    });

p/s:我只是硬编码 5x5 尺寸 谢谢。

【问题讨论】:

  • 也粘贴您的布局代码。
  • @Haresh:你能教我如何使用双向gridview吗?谢谢
  • 请从 github 下载 zip 并从中获取示例代码。
  • @Haresh:好的。但是,我阅读了描述,也许它不适用于这种情况

标签: android android-layout android-gridview


【解决方案1】:

要获取GridView 中的列数,请使用方法getNumColumns()

例如:

gridview.getNumColumns();

http://developer.android.com/reference/android/widget/GridView.html#getNumColumns()

【讨论】:

  • 也许,你误解了我的问题。我不想获取 GridView 中的列数。我想如何“确定GridView中一行的项目数”
  • 您将什么定义为“项目”? GridView 通常表示每行每列一个项目。所以一个 5x5 的网格是 5 行 5 列,包含 25 个项目。
  • 我只是按照这个谷歌指南通过 GridView developer.android.com/guide/topics/ui/layout/gridview.html 绘制矩阵。对于项目定义,您可以阅读本指南。
  • 好的,这是一个标准的网格视图,我的回答仍然有效。每行包含 getNumColumns() 项数。我问你你叫什么物品?对你来说是什么?你想确定什么?根据您的描述,您想知道每行膨胀了多少网格视图项。该数字等于网格中的列数。
  • 一个项目是一个圆圈。 “该数字等于网格中的列数”是的,我知道。但是,正如我所说,一个项目的尺寸不足以连续 5 个(我将 GridView numColumns=auto-fit 设置为 5,因为该值在运行时更改)。如果我设置 numColumns=5,项目是混合的(项目 2 覆盖项目 1 的一部分,项目 3 覆盖项目 2 的一部分,...)。这就是为什么我想调整它的大小以适应所有情况。
【解决方案2】:

要获取 GridLayout 中的项目数,请使用 getChildCount() 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多