Android GridView加速滚动

时间:2012-02-03 03:21:46

标签: android performance gridview scroll

我有一个GridView,其中每个单元格都包含一个带有特定文本颜色和特定背景图像的数字(参见图像)。

生成它的getView()方法如下所示。 可以提高滚动速度吗?

enter image description here

public View getView(int position, View convertView, ViewGroup parent) {
    int row = getRow(position);
    int col = getCol(position);

       TextView view;
       if (convertView == null) {
           view = new TextView(context, null);
           view.setGravity(Gravity.CENTER);

           if (answer[row][col] == ROW_MARKER) {
               view.setTextColor(Color.YELLOW);
               view.setBackgroundResource(R.drawable.outline_whiteonblue);
           }
           else {
               view.setTextColor(Color.WHITE);
                       view.setBackgroundResource(R.drawable.outline_whiteonblack);
            }
        }               
        else 
           view = (TextView) convertView;   

        if (answer[row][col] == ROW_MARKER) {
               view.setTextColor(Color.YELLOW);
               view.setBackgroundResource(R.drawable.outline_whiteonblue);
               view.setText(Integer.toString(getRow(position) + 1));
        }
        else {
            view.setTextColor(Color.WHITE);
            view.setBackgroundResource(R.drawable.outline_whiteonblack);
            view.setText(Character.toString(answer[row][col]));
        }
        return view;
 }

1 个答案:

答案 0 :(得分:2)

编辑:在此更改我的代码。我认为上面的代码有点多余,但不然。

public View getView(int position, View convertView, ViewGroup parent) {
        int row = getRow(position);
        int col = getCol(position);

TextView view;
if (convertView == null) {
    view=new TextView(context,null);
    view.setGravity(Gravity.CENTER);
} else {
   view=(TextView)convertView;
} 


if (answer[row][col] == ROW_MARKER) {
            view.setTextColor(Color.YELLOW);
            view.setBackgroundResource(R.drawable.outline_whiteonblue);
            view.setText(Integer.toString(getRow(position) + 1));
 } else {
            view.setTextColor(Color.WHITE);
            view.setBackgroundResource(R.drawable.outline_whiteonblack);
            view.setText(Character.toString(answer[row][col]));
 }

return view;
}