android gridview没有显示顶行

时间:2014-07-29 01:10:32

标签: android gridview

我在relativelayout中使用gridview,我试图让整个网格显示在屏幕上。顶行没有完全显示..我只是显示右上角的按钮。网格是4列6行。除了屏幕顶部的所有行外,所有行都会显示。我的代码如下:

<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>

我的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

}

}

和我的适配器类:

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;
}

}

这里的想法是让网格最终垂直滚动(用for循环添加更多按钮),我希望它在网格中的某些按钮消失时滚动。在移动之前,我希望能够首先正确地使用按钮绘制网格。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您需要自定义GridView或使用smoothScrollToPosition来显示第一行。

当你向GridView添加元素时,它会滚动。如果您的按钮太大而无法占用可用空间,它将向下滚动。同样,如果您指定GridView的高度,它将截断按钮。

因此,您需要测量屏幕上的可用空间,然后相应地设置按钮高度。或者你可以在构建它之后滚动到顶部并允许它垂直滚动,这听起来像你想要的。

从根本上说,GridView滚动,因此加载的元素数量超过屏幕上最多的元素应该会导致您看到的内容。重置锚点位置或调整元素大小是您唯一的选择。