如何为GridLayout创建ItemView

时间:2016-06-14 09:04:39

标签: android android-gridlayout

我尝试为此创建网格布局,我使用下面的代码。但在该代码中,我不知道什么是ItemView以及如何实现它。当我把这个代码放在我的项目中时,我得到错误无法解析符号" ItemView"。我从下面的链接获得了这段代码 -

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

        }

    }

1 个答案:

答案 0 :(得分:0)

我发现创建GridView的最佳方法是使用RecyclerView和GridLayouy管理器。适配器实现与普通RecyclerView

相同

您可以像这样创建GridLayout管理器

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

这将帮助您提高性能并使用ViewHolder模式创建视图

如果你从未使用过RecyclerView,你可以看到一个很好的例子here