Android:具有图像和文本错误的自定义网格视图适配器

时间:2016-02-16 09:05:52

标签: android

我正在创建一个名为Facility的片段,其中包含GridView个图片和TextView。为此,我创建了facilityfragment和自定义适配器类以及两个xml文件。

  1. row_grid

  2. gridview.xml。

  3. 在自定义适配器中,我可以显示图像,但如何将其与row_grid链接并在其中显示文字。例如;在网格视图中,将有图像,并在其下方描述每个图像TextView。以下是代码段

    fragment_facility_grid.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >
    
        <GridView
            android:id="@+id/gridViewCustom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:columnWidth="80dp"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" />
    
    
    </RelativeLayout>
    

    row_grid.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp" >
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
             >
        </ImageView>
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textSize="15sp" >
        </TextView>
    
    </LinearLayout>
    

    FacilityFragment

    public class FacilityFragment extends Fragment  {
        private GridView gridView;
        private ImageView imageView;
    
    
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            View view = inflater.inflate(R.layout.fragment_facility_grid, container,
                    false);
            GridView gridView = (GridView) view.findViewById(R.id.gridViewCustom);
            gridView.setAdapter(new ImageAdapter(view.getContext()));
    
            return view;
    
    }
    
    }
    

    CustomGridViewAdapter

    public class CustomGridViewAdapter extends BaseAdapter {
        private Context mContext;
    
    
        // Keep all Images in array
        public Integer[] mThumbIds = {
                R.drawable.bank, R.drawable.facility,
                R.drawable.gym , R.drawable.facility,
                R.drawable.facility , R.drawable.construct1,
    
        };
    
        public String[] mThumbNames = {
                "Bank", "Facility","Gym", "Facility","Facility","Construct"
    
        };
    
        // Constructor
        public CustomGridViewAdapter(Context c){
            mContext = c;
        }
    
        @Override
        public int getCount() {
            return mThumbIds.length;
        }
    
        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
            return imageView;
        }
    
    }
    

    因为我是Android新手所以面临这样的困难。

    1. customadapter链接到row_grid

    2. 在每张图片下方显示文字

3 个答案:

答案 0 :(得分:2)

首先更改此行

 gridView.setAdapter(new ImageAdapter(view.getContext()));

 gridView.setAdapter(new CustomGridViewAdapter(view.getContext()));

和CustomGridViewAdapter.java:

public class CustomGridViewAdapter extends BaseAdapter {
    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.bank, R.drawable.facility,
            R.drawable.gym, R.drawable.facility,
            R.drawable.facility, R.drawable.construct1,

    };
    public String[] mThumbNames = {
            "Bank", "Facility", "Gym", "Facility", "Facility", "Construct"

    };
    private Context mContext;

    // Constructor
    public CustomGridViewAdapter(Context c) {
        mContext = c;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


//        ImageView imageView = new ImageView(mContext);
//        imageView.setImageResource(mThumbIds[position]);
//        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//        imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
        ViewHolderItem viewHolder;


        if (convertView == null) {

            // inflate the layout
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(R.layout.row_grid, parent, false);

            // well set up the ViewHolder
            viewHolder = new ViewHolderItem();
            viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textView);

            viewHolder.imageViewItem = (ImageView) convertView.findViewById(R.id.imageView);
            // store the holder with the view.
            convertView.setTag(viewHolder);


        } else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (ViewHolderItem) convertView.getTag();
        }

        // object item based on the position

        // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
        viewHolder.textViewItem.setText(mThumbNames[position]);
        viewHolder.textViewItem.setTag(position);

        viewHolder.imageViewItem.setImageResource(mThumbIds[position]);


        return convertView;

    }

    // our ViewHolder.
// caches our TextView
    static class ViewHolderItem {
        TextView textViewItem;
        ImageView imageViewItem;
    }


}

您没有使用在XML布局中定义的ImageView和TextView,而您只是在创建新的ImageView。

希望这能帮助您理解

答案 1 :(得分:0)

这里有教程如何在android中使用网格,在第4点你已经指定了如何将适配器设置为GridView 同样在getView的第5点中,Overriden方法显示了如何将文本填充到单元格 http://developer.android.com/guide/topics/ui/layout/gridview.html

答案 2 :(得分:0)

当你设置适配器调用你的适配器时

gridView.setAdapter(new CustomGridViewAdapter(view.getContext()));

在您必须在适配器的 getView()方法中设置 row_grid.xml 之后,希望对此有所帮助

      @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_grid.xml, parent, false);
                }
            ImageView imgView= (ImageView) convertView.findViewById(R.id.imageView);
TextView txtView= (TextView) convertView.findViewById(R.id.txtView);
            imgView.setImageResource(mThumbIds[position]);
txtView.setText(mThumbNames [position]);
            return convertView ;
        }