Android:为Gridview网格使用两个不同的适配器

时间:2016-02-14 17:34:07

标签: android gridview android-arrayadapter android-image

我想知道是否可以为某个4X4维网格应用两个不同的适配器(ArrayAdapter用于String数组,ImageAdapter用于数组中每个值的背景)...我问这个因为我的实际(代码)下面只是一个示例)程序包含用户输入的高度和宽度值(https://stackoverflow.com/questions/35382979/android-auto-fitting-row-height-of-gridview-based-on-user-inputted-values),而不是硬编码。比方说,如果我想将棕色块分配为奇数的背景,并使用grid.setAdapter()为两个适配器分配偶数的灰色块...我将如何使用Android Studio进行编码?

以下是网格的Java代码:

package dpark.sample;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private int height, width;
    String[] list;
    GridView grid;

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

        height = 4;
        width = 4;

        buildList();

        grid = (GridView)findViewById(R.id.gridView);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, list);
        grid.setAdapter(adapter);
        grid.setNumColumns(width);

        //***TEMPORARILY COMMENTING THE FOLLOWING OUT SINCE THIS WILL JUST OVERWRITE THE
        // ARRAYADAPTER***
        //grid.setAdapter(new ImageAdapter(getApplicationContext()));
    }

    private void buildList() {
        int tempIncrementor = 1;
        int dimensions = height * width;

        list = new String[dimensions];

        for (int i = 0; i < dimensions; i++) {
            list[i] = String.valueOf(tempIncrementor);

            tempIncrementor++;
        }
    }

    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
        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(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

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

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.brownblock,
                R.drawable.grayblock
        };
    }
}

...虚拟仿真器中的哪些输出:

enter image description here

...至于输出颜色块图像,我暂时在我的代码中注释掉了ArrayAdapter块,因此我可以输出以下内容:

enter image description here

1 个答案:

答案 0 :(得分:0)

不,你不能设置两个适配器,因为方法setAdapter会覆盖前一个。

但是你的目标不需要它,你应该将值存储在arraylist中并在getView方法中检索它们。