0

I tried to create Grid Layout for this I used below code. But in that code I don't know what is ItemView and how to implement it. When I put this code in my project I get error can not resolve symbol "ItemView". I got this code from below link -

https://stackoverflow.com/a/36143314/5726392

public class PrivateSeatViews extends FrameLayout implements View.OnClickListener {

        private GridLayout mGridView;
        private int mRowsCount;
        private int mColsCount;
        private int mCellSpace;
        private OnItemClickListener mOnItemClickListener;

        public PrivateSeatViews(Context context) {
            super(context);
            init(context, null);
        }

        // other constructors

        private void init(Context context, AttributeSet attrs) {
            // default values

            View layout = inflate(getContext(), R.layout.grid_layout, null);
            mGridView = (GridLayout) layout.findViewById(R.id.Lower);
            mGridView.setRowCount(mRowsCount);
            mGridView.setColumnCount(mColsCount);
            mGridView.post(new Runnable() {
                @Override
                public void run() {
                    int width = getMeasuredWidth() / getColumnsCount();
                    int height = getMeasuredHeight() / getRowsCount();
                    for (int i = 0; i < getRowsCount(); i++) {
                        for (int j = 0; j < getColumnsCount(); j++) {
                            GridLayout.LayoutParams params = (GridLayout.LayoutParams) getChildAt(i, j).getL;
                            params.width = width;
                            params.height = height;
                            getChildAt(i, j).setLayoutParams(params);
                        }
                    }
                }
            });
            addView(layout);
        }

        // this method allows to dinamically create grid
        public void buildChildren(int rowsCount, int colsCount) {
            mRowsCount = rowsCount;
            mColsCount = colsCount;
            mGridView.setRowCount(mRowsCount);
            mGridView.setColumnCount(mColsCount);
            buildChildren();
        }

        public void buildChildren() {
            for (int i = 0; i < getRowsCount(); i++) {
                for (int j = 0; j < getColumnsCount(); j++) {
                    ItemView1 view = new ItemView1(getContext(), i, j);
                    view.setOnClickListener(this);
                    mGridView.addView(view);
                }
            }
        }

        public void setOnItemClickListener(OnItemClickListener listener) {
            mOnItemClickListener = listener;
        }

        public ItemView getChildAt(int rowIndex, int columnIndex) {
            int index = (getColumnsCount() * rowIndex) + columnIndex;
            return (ItemView) mGridView.getChildAt(index);
        }

        public boolean isTouchOn(int rowIndex, int columnIndex) {
            return getChildAt(rowIndex, columnIndex).isTouchOn();
        }

        public int getColumnsCount() {
            return mGridView.getColumnCount();
        }

        public int getRowsCount() {
            return mGridView.getRowCount();
        }

        @Override
        public void onClick(View v) {
            if (v instanceof ItemView) {
                ItemView view = (ItemView) v;
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view);
                }
            }
        }

        public interface OnItemClickListener {

            void onItemClick(ItemView view);

        }

    }
Community
  • 1
  • 1
Varshil shah
  • 268
  • 1
  • 6
  • 19
  • What are you trying to achieve, why do you need to use that code to create a gridview? Can't you just define it in an xml file? – Dmitri Borohhov Jun 14 '16 at 09:30
  • i will try to create bus seat layout so for that i used grid layout and seats come dynamically so i create gridlayout programmatically – Varshil shah Jun 14 '16 at 09:33
  • ItemView is nothing but it is view control in which you have to bind data. So first specify which kind of data you have only image or only text or both text and image? Accordingly your itemview will be decide – Vickyexpert Jun 14 '16 at 09:33
  • i have imageview and textview both...if u have no problem then give example.. – Varshil shah Jun 14 '16 at 09:34
  • Try below link for better understanding . https://www.caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html – Vickyexpert Jun 14 '16 at 09:44
  • i want to use griddlayout not gridview. – Varshil shah Jun 14 '16 at 10:49

1 Answers1

0

The best way i found to create the GridView is to use RecyclerView with the GridLayouy manager. Adapter implementations is the same as for normal RecyclerView

You can create GridLayout manager like this

//3 is the row span
GridLayoutManager a = new GridLayoutManager(this, 3);

This will help you to improve performance and create your view with the ViewHolder pattern

If you never used RecyclerView you can see a fine example here

Ivan Milisavljevic
  • 2,048
  • 2
  • 13
  • 21