Rob
Rob

Reputation: 762

Android GridView Speed Up Scrolling

I have a GridView where each cell contains a number with a particular text color and a particular background image (see image).

The getView() method that generates it is below. Can the scrolling speed be improved?

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

Upvotes: 0

Views: 1223

Answers (1)

Fraggle
Fraggle

Reputation: 8727

EDIT: Changing my code here. I think your code above is just a bit redundant but otherwise ok.

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

Upvotes: 2

Related Questions