Android GridLayout only shows last child

好久不见. 提交于 2020-01-16 01:09:20

问题


I am new to Android Development. I have been working with using a GridLayout to display Dynamically inserted ImageViews.

My issue is located in "onFocusWindowChanged" but I pasted my onCreate where I do my assignments of the images.

private List<Behavior> behaviors = null;
private static int NUM_OF_COLUMNS = 2;
private List<ImageView> images;
private GridLayout grid;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_behaviors);

    XMLPullParserHandler parser = new XMLPullParserHandler();

    try {
        behaviors = parser.parse(getAssets().open("catagories.xml"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    grid = (GridLayout) findViewById(R.id.behaviorGrid);
    images = new ArrayList<ImageView>();

    grid.setColumnCount(NUM_OF_COLUMNS);
    grid.setRowCount(behaviors.size() / NUM_OF_COLUMNS);

    for (Behavior behavior : behaviors)
        images.add(this.getImageViewFromName(behavior.getName()));

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);
    View view = (View) findViewById(R.id.scrollView);

    int width = (int) (view.getWidth() * .45);
    Log.i("ViewWidth", Integer.toString(width));

    GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
    lp.height = width;
    lp.width = width;

    int childCount = images.size();

    ImageView image;

    for (int i = 0; i < childCount-1; i++) {

        image = images.get(i);
        image.setLayoutParams(lp);      
        grid.addView(image);

    }

}

In my (short) previous experience, using

grid.add(View); 

worked fine, but now I am only seeing the last child display only. Looking through the debugger I can see that the gridview is being populated with more than just the last element, as well as the last imageview.

Thank you for your help


回答1:


you should create a GridLayout.LayoutParams for each ImageView:

for (int i = 0; i < childCount-1; i++) {
    GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
    lp.height = width;
    lp.width = width;

    ......
}

GridLayout.LayoutParams contains location information, e.g [column:2, row:3]. In your code, all ImageViews are set the same GridLayout.LayoutParams, so they are located in the same cell(overlapping each other).

When use LinearLayout.LayoutParams instead, there is no location information in it. GridLayout will create a new GridLayout.LayoutParams for each child view, so all ImageViews use their own different GridLayout.LayoutParams and location.

Wish this help. You can read the GridLayout.java and ViewGroup.java for more details.




回答2:


So I solved my issue, although I'm not sure how-

GridLayout.LayoutParams lp = new GridLayout.LayoutParams();

changed to...

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(x,y);

made it work just as I wanted it. But I'm not sure why- If anyone could explain, please do :)



来源:https://stackoverflow.com/questions/31815732/android-gridlayout-only-shows-last-child

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