简体   繁体   English

Android Gridview不能很好地显示位置0的视图

[英]Android Gridview not showing view at position 0 well

I'm new to android development and I'm facing a small difficulty regarding the GridView . 我是android开发的新手,并且在GridView方面面临着一个小难题。 I have a Gridview in my first class called Game and I added a BaseAdapter called GameAdapter . 我有一个Gridview我叫做Game一流,我增加了一个BaseAdapter称为GameAdapter The code is working fine but when adding the views to the GridView , it doesn't show the first element (at position 0). 代码工作正常,但是将视图添加到GridView ,它不显示第一个元素(位置0)。

public View getView(int position, View convertView, ViewGroup parent) {

    Button b;
    if (convertView == null) {
        b = new Button(mContext);
        b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
        //b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);
    } else {
        b = (Button) convertView;
    }
    Drawable d = new BitmapDrawable(Game.splittedBitmaps[position]);
    b.setBackground(d);
    b.setId(position);
    b.setTag("Image_" + position);
    b.setOnClickListener(Game.s);
    Game.buttons[position] = b;
    return b;
}

Assume: mContext is the Context of the Game class, cols and raw are static integers in the Game class representing number go columns and rows respectively, gv is the actual GridView in Game, Game.splittedBitmaps[] is an array containing the Bitmaps to be set as backgrounds. 假设: mContext是Game类的Context,cols和raw是Game类中的静态整数,分别表示行数和行数, gv是Game中的实际GridViewGame.splittedBitmaps[]是一个包含要Game.splittedBitmaps[]的位图的数组。设置为背景。

So this is my getView() code. 这是我的getView()代码。 Note that when I have the line code: 请注意,当我有行代码时:

b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));

This will be the result: 结果将是:

在此处输入图片说明

http://postimg.org/image/794mrqpnn/ http://postimg.org/image/794mrqpnn/

Note the missing view at position 0. 注意在位置0缺少的视图。

In my other run, after I removed the line code: 在我的其他运行中,删除行代码后:

b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));

the picture was shown but the height got shrank. 图片显示了,但身高缩小了。

在此处输入图片说明

http://postimg.org/image/nmomaw5sz/ http://postimg.org/image/nmomaw5sz/

When I added the line code: 当我添加行代码时:

b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);

the result became half the way between result 1 and result 2: 结果变为结果1和结果2之间的一半:

在此处输入图片说明

postimg.org/image/ubv5qwr4z/ postimg.org/image/ubv5qwr4z/

I just want to get the picture at position 0 with the same dimensions of the others. 我只想在其他位置具有相同尺寸的位置0获得图片。

Edited: 编辑:

XML of the grid view I'm using: 我正在使用的网格视图的XML:

<GridView
    android:id="@+id/gvGame"
    android:layout_width="fill_parent"
    android:layout_height="375dp"
    android:gravity="center"
    android:horizontalSpacing="3dp"
    android:padding="3dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" />

And this the code regarding the grid view in the Game class: 这是有关Game类中的网格视图的代码:

gv = (GridView) findViewById(R.id.gvGame);
gv.setNumColumns(cols);
gv.setAdapter(new GameAdapter(getApplicationContext()));

Im pretty sure you want your code to look as follows, whether this truly answers your question or not. 我非常确定您希望您的代码如下所示,无论这是否确实回答了您的问题。

public View getView(int position, View convertView, ViewGroup parent) {
    Button b;
    if (convertView == null) {
        b = new Button(mContext);
        b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
        //b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);

        // This code from below SHOULDNT need to be done if the view already exists
        // Im also assuming that the drawable WILL NOT change, if it does, the 2
        // lines below may move outside this if statement
        Drawable d = new BitmapDrawable(Game.splittedBitmaps[position]);
        b.setBackground(d);


        b.setId(position);
        b.setTag("Image_" + position);
        b.setOnClickListener(Game.s);
        Game.buttons[position] = b;
    } else {
        b = (Button) convertView;
    }

    return b;
}

Pretty old post but if anyone get the same issue, here is the possible reason and solution: 很老的帖子,但是如果有人遇到同样的问题,这是可能的原因和解决方案:

You are probably setting the adapter and its list in the creation cycle of the fragment/activity (onCreate, onStart, etc..), therefore when the first view creation is done inside your adapter the GridView is not drawn yet and has a width of 0. 您可能在片段/活动的创建周期(onCreate,onStart等)中设置了适配器及其列表,因此,当在适配器内部完成第一次视图创建时,尚未绘制GridView,其宽度为0。

It does not happen to the other created views probably because the creation of the first one forced the GridView to be drawn. 创建的其他视图不会发生这种情况,可能是因为第一个视图的创建迫使绘制了GridView。

You can find a bunch of solutions on that post: https://stackoverflow.com/a/24035591/3495069 您可以在该帖子上找到很多解决方案: https : //stackoverflow.com/a/24035591/3495069

I've personnaly ended up with the post method: 我个人经历了post方法:

    mGridView.post(new Runnable() {
        @Override
        public void run() {
            mGridView.setAdapter(mAdapter);
        }
    });

I had this same problems too. 我也有同样的问题。 Here's what it'd looks like if it's your code. 这就是您的代码。

if (convertView == null) {
    b = new Button(mContext);
    b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
    //b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);
} else {
    b = (Button) convertView;
    if (b.getHeight() == 0)
        b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM