【问题标题】:Display a layout as item in GridView在 GridView 中将布局显示为项目
【发布时间】:2013-11-10 23:19:45
【问题描述】:

此图像显示了我想要实现的布局,黑色方块是可绘制文件夹中的图像,红色方块是一个框架布局,其中 2 个图像相互叠加。问题是这样的:

红色方块(框架布局)没有出现。请参阅下面的代码...

 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.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

我从Android Developers 得到了这个运行良好的代码,它在我的程序中运行良好,问题是我想更改这一行中的一些项目:

private Integer[] mThumbIds = {
    R.drawable.sample_2, R.drawable.sample_3,
    R.drawable.sample_4, R.drawable.sample_5,
    R.drawable.sample_6, R.drawable.sample_7
}; 

我将R.drawable.sample_2 更改为R.layout.some_layout

这里是some_layout.xml

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

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="115dp"
        android:layout_margin="2dp"
        android:layout_weight="0.33" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/imagebehind" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/frameimage" />
    </FrameLayout>

</LinearLayout>

出现下一行的所有项目:

private Integer[] mThumbIds = {
  R.layout.some_layout, R.drawable.sample_3,
  R.drawable.sample_4, R.drawable.sample_5,
  R.drawable.sample_6, R.drawable.sample_7
};

R.layout.some_layout 除外。

【问题讨论】:

  • 这个R.layout.some_layout 你需要提供一个drawable。所以它错了
  • 有没有办法可以使用布局而不是可绘制项目? @Raghunandan
  • 您可以为 gridview 项目添加自定义布局,但不能用这样的布局替换可绘制对象
  • 是这样吗?好的,谢谢@Raghunandan,我会这样做的。

标签: android xml layout gridview


【解决方案1】:
// try this
<?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" >

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="115dp"
        android:layout_margin="2dp"
        android:layout_weight="0.33" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

</LinearLayout>

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item,null,false);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.button = (Button) convertView.findViewById(R.id.button);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }

            holder.image.setImageResource(mThumbIds[position]);
            return convertView;
        }

        private Integer[] mThumbIds = {
                R.layout.some_layout, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7
        };

        class ViewHolder {
            ImageView image;
            Button button;
        }
    }

【讨论】:

  • tnx @Haresh ,但是框架布局仍然没有出现,你所做的只是在项目中放置了一个按钮。但谢谢强硬:)
【解决方案2】:
package com.example.sampleproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater layoutInflater;

    public ImageAdapter(Context c) {
        mContext = c;
        layoutInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;
        Button button;


        if (convertView == null) {  // if it's not recycled, initialize some attributes
            convertView = layoutInflater.inflate(R.layout.someLayout, null);
            imageView = (ImageView)convertView.findViewById(R.id.image);
            button = (Button)convertView.findViewById(R.id.button);

        } else {

        }
        button.setText("some text");
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

朋友很容易膨胀布局:阅读教程基础适配器会很有用

【讨论】:

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