GridLayout with view dynamic get row/column

时光总嘲笑我的痴心妄想 提交于 2019-12-02 04:04:55

For simplifying, you can implement a custom Board view wrapping the GridLayout and related logic. Below I report a possible approach.

Expectation here is to have an ItemView for representing one single cell in the board.

public class Board extends FrameLayout implements View.OnClickListener {

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

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

    // other constructors

    private void init(Context context, AttributeSet attrs) {
        // default values
        mRowsCount = 1;
        mColsCount = 1;
        View layout = inflate(getContext(), R.layout.view_lights_board, null);
        mGridView = (GridLayout) layout.findViewById(R.id.view_grid);
        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).getLayoutParams();
                        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++) {
                ItemView view = new ItemView(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);

    }

}

In your Activity layout you will have something like this (here I assume your app package is com.android.example):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.android.example.Board
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="400dp" />

</FrameLayout>

And this is possible implementation of the Activity:

public class MainActivity extends AppCompatActivity implements LightsOutBoard.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Board board  = (Board) findViewById(R.id.grid);
        board.setOnItemClickListener(this);

        board.buildChildren(3, 3);
    }

    @Override
    public void onItemClick(ItemView view) {
        String text = view.getRowIndex() + " - " + view.getColumnIndex();
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

}

Hope this could help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!