My final objective is to be able to drag a view and drop it into a grid-oriented layout (ie a layout that restricts the placement of these views to cells, instead of anywhere).
I'm trying to do that with GridLayout
, as it seemed appropriate, but I am now doubting if it is possible to do what I want using it.
When handling the drop, I try to add views to the GridLayout
as such:
GridLayout.Spec row = GridLayout.spec(4);
GridLayout.Spec column = GridLayout.spec(2);
LayoutInflater inflater = LayoutInflater.from(gridLayout.getContext());
gridLayout.addView(inflater.inflate(R.layout.my_view, null), new GridLayout.LayoutParams(row, column));
The view does get added to the GridLayout
, but not to the right cell (which would be, for the code above, the cell on the 5th row, 3rd column).
How can I do this correctly? Should I try something else besides GridLayout
? GridLayout
seemed appropriate because I also want to be able to place views with different sizes in it (ie views that would span more or less cells).
Thank you.
Edit: I am considering using a RelativeLayout
and adding views to it like so:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
relativeLayout.addView(myView, params);
This way, I can specify the exact coordinates (eg (50;60)
) of the view.
As i can see the api does not provide this functionality, but you can do the following:
you can set:
mGridLayout.setOrientation(GridLayout.HORIZONTAL);
or
mGridLayout.setOrientation(GridLayout.VERTICAL);
to organize how the
mGridLayout.addView(view)
adds the childs in the grid. Using the first method (default) the childs are filled by columns first, whereas the second by rows first
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.