简体   繁体   English

android gridview不显示第一行

[英]android gridview not showing top row

I am using a gridview inside a relativelayout and im trying to make the whole grid show up on the screen. 我在相对布局中使用了gridview,我试图使整个网格显示在屏幕上。 The top row is not completely showing..I am only getting the top right button to show. 第一行未完全显示。.我仅显示右上角的按钮。 The grid is 4 columns and 6 rows. 网格为4列6行。 All of the rows show up besides the one at the top of the screen. 除了屏幕顶部的所有行以外,所有行均显示。 My code is as follows: 我的代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:columnWidth="20dp"
    android:horizontalSpacing="1dp"
    android:numColumns="4"
    android:verticalSpacing="1dp" >
</GridView>

and my main.java is: 而我的main.java是:

public class MainActivity extends Activity  implements OnClickListener 
{
private long startTime = 0L;
private Handler customHandler = new Handler();
private int game_running = 0;
private int button_clicks = 0;
private int previous_button_id = 0;
private int current_button_id = 0;
private CharSequence button_value = null;
private CharSequence prevbutton_value = null;
private int j = 0;
private int isgameover = 0;
boolean flag = false;
boolean reset = false;
long gametime = 0;
long resettime = 0;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
boolean resetbool = false;
private ArrayList<Button> mButtons = new ArrayList<Button>();
private ArrayList<Button> comp_buttons = new ArrayList<Button>();
private int[] numbers =
{
        1,
        1,
        2,
        2,
        3,
        3,
        4,
        4,
        5,
        5,
        6,
        6,
        7,
        7,
        8,
        8,
        9,
        9,
};
Animation fadeout;

protected void onSaveInstanceState(Bundle savedInstanceState) 
{
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean("Reset", true);;
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    shuffle(numbers);
    fadeout = AnimationUtils.loadAnimation(this,R.anim.fadeout);
    Button cb = null;
    for (int i=0; i<24; i++) 
    {
        cb = new Button(this);
        cb.setId(i);
        cb.setOnClickListener(this);
        cb.setText(Integer.toString(numbers[j]));
        cb.setTextSize(0);
        cb.setBackgroundResource(R.drawable.button);
        cb.setHeight(100);
        j++;
        registerForContextMenu(cb);
        mButtons.add(cb);
        if(j >= 18)
        {
            j = 0;
        }
    }
    GridView gridView = (GridView) findViewById(R.id.grid);
    gridView.setAdapter(new CustomAdapter(mButtons));
}


  static void shuffle(int[] ar)
  {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

} }

and my adapter class: 和我的适配器类:

public class CustomAdapter extends BaseAdapter {
private ArrayList<Button> mButtons = null;

public CustomAdapter(ArrayList<Button> b)
{
    mButtons = b;
}

@Override
public int getCount() 
{
    return mButtons.size();
}

@Override
public Object getItem(int position) 
{
    return (Object) mButtons.get(position);
}
@Override
public long getItemId(int position) 
{
    //in our case position and id are synonymous
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    Button button;
    if (convertView == null) 
    {
        button = mButtons.get(position);
    } 
    else 
    {
        button = (Button) convertView;
    }
    if( (position == 0) || (position == 1) || (position == 2) )
    {
        button.setVisibility(convertView.GONE);
    }
return button;
}

} }

The idea here is to have the grid eventually scroll vertically (add more buttons with for loop) and I want it to scroll when some of the buttons in the grid disappear. 这里的想法是使网格最终垂直滚动(使用for循环添加更多按钮),并且我希望网格中的某些按钮消失时进行滚动。 I wanted to be able to draw the grid with buttons correctly first before moving on. 我希望能够在继续之前先正确地绘制带有按钮的网格。 Thanks for the help. 谢谢您的帮助。

You will need a custom GridView or use smoothScrollToPosition to show the top row. 您将需要一个自定义GridView或使用smoothScrollToPosition来显示第一行。

As you add elements to the GridView , it will scroll. 将元素添加到GridView ,它将滚动。 If your buttons are too large for the available space, it will scroll down. 如果按钮对于可用空间而言太大,则会向下滚动。 Likewise, if you specify the height of the GridView it will truncate the buttons. 同样,如果指定GridView的高度,它将截断按钮。

So, you need to measure the available space on the screen, then set your button heights accordingly. 因此,您需要测量屏幕上的可用空间,然后相应地设置按钮高度。 Or you can scroll to the top after building it and allow it to scroll vertically, which sounds like what you want anyway. 或者,您也可以在构建后滚动到顶部并允许其垂直滚动,这听起来还是您想要的。

Fundamentally, a GridView scrolls, so loading it with more elements than can fit on the screen initially should result in what you are seeing. 从根本上讲, GridView滚动,因此,在加载GridView ,它所包含的元素数量可能超出屏幕上最初容纳的数量,这将导致您看到的内容。 Reset the anchor position or resize your elements are your only options. 重置锚点位置或调整元素大小是您唯一的选择。

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

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