使用GridView的自定义适配器

时间:2015-07-23 16:15:31

标签: android android-gridview

我们可以在Gridview中设置图片,而无需使用 CustomAdapter 。我的意思是我们可以直接使用ArrayAdapter设置预定义的GridView。 如下面的代码

GridView gridview_object;

ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,into,int[]);

gridview_object.setadapter(adapter);

类似的东西......它会起作用吗?

4 个答案:

答案 0 :(得分:2)

我不这么认为,但实现 ImageAdapter 非常简单,GridView文档中的this page包含 ImageAdapter <的实现/ strong>,看看吧。

答案 1 :(得分:2)

我不能完全同意之前的答案,因为我相信我们可以在不使用自定义适配器的情况下创建GridView(带图像和文本)。这有点棘手但仍然很可能。参见示例

   // Array of strings storing titles
    String[] titles = new String[] {
        "title1",
        "title2"
    };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] icons = new int[]{
        R.drawable.icon1,
        R.drawable.icon2
    };

    //bind the icons & titles array inside a loop using HashMap so that
    // we can refer the keys & values in a single array for adapter
            List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            for(int i=0;i<2;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
                hm.put("title", titles[i]);
                hm.put("icon", Integer.toString(icons[i]) );
                aList.add(hm);
            }

             // refer the stored key & value of hashmap inside a single array 

            // Keys used in Hashmap
            String[] from = { "icon","title"};
            // Ids of views in gridviewview_layout
            int[] to = { R.id.icon,R.id.title};

            // Instantiating an adapter to store each items
            // R.layout.gridview_layout defines the layout of each item
            // set the single array contains the icons & titles in SimpleAdapter
           // 'from' refers the keys & 'to' refers the ids where the data will be displayed
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridview_layout, from, to);

            // Getting a reference to gridview of MainActivity
            GridView gridView = (GridView) findViewById(R.id.gridview);

            // Setting an adapter containing images to the gridview
            gridView.setAdapter(adapter);
包含GridView的

activity_main布局

<GridView 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:id="@+id/gridview"
    android:numColumns="auto_fit"
/>

gridview_layout布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_gravity="center"
>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
    />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:gravity="center_horizontal"
    />

</LinearLayout>

答案 2 :(得分:1)

如果您尝试使用自定义布局,请否。

如果您想要一个自定义布局,必须使用自定义适配器,将自定义布局 XML 文件的每个组件绑定到一个变量中,以适应每个值< strong>网格/循环器/列表视图(onCreate()方法或onCreateViewHolder()RecyclerView)。

答案 3 :(得分:0)

,这可以通过使用SimpleAdapter来实现 请查看SimpleAdapter here的源代码。

公共构造函数是

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to)

此处最后一个参数是您提供的ID数组。

现在看一下方法

private void bindView(int position, View view) 

您将看到该方法检查每个ids(to)中的找到的视图,无论它是CheckableTextView还是ImageView的实例,并设置相应的映射值的值

基本上是行 -

                   if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else if (v instanceof TextView) {
                            // Note: keep the instanceof TextView check at the bottom of these
                            // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                            setViewText((TextView) v, text);
                        } else {
                            throw new IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " +
                                    (data == null ? "<unknown type>" : data.getClass()));
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }