使用滚动时,网格视图按钮会失真

时间:2012-07-04 14:34:15

标签: java android button gridview scroll

public View getView(int position, View convertView, ViewGroup parent) {
        Button btn;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes

            btn = new Button(mContext);
            KeypadButton keypadButton = mButtons[position];

            switch(keypadButton.mCategory)
            {
            case MEMORYBUFFER:
                btn.setBackgroundResource(R.drawable.keypadmembuffer1);
                break;  
            case CLEAR:
                btn.setBackgroundResource(R.drawable.keypadclear1);
                break;  
            case NUMBER:
                btn.setBackgroundResource(R.drawable.keypad1);
                break;
            case OPERATOR:

                btn.setBackgroundResource(R.drawable.keypadop1);
                break;
            case OTHER: 
                btn.setBackgroundResource(R.drawable.keypadother1);
                break;
            case RESULT:
                btn.setWidth(8000);
                btn.setBackgroundResource(R.drawable.keypadresult1);
                break;
            case DUMMY:
                btn.setBackgroundResource(R.drawable.appvertical1);
                break;
            default:
                btn.setBackgroundResource(R.drawable.keypad1);
                break;
            }
            // Set OnClickListener of the button to mOnButtonClick
            if(keypadButton != KeypadButton.DUMMY)
                btn.setOnClickListener(mOnButtonClick);
            else
                btn.setClickable(false);
            // Set CalculatorButton enumeration as tag of the button so that we
            // will use this information from our main view to identify what to do
            btn.setTag(keypadButton);
        } else {
            btn = (Button) convertView;
        }

        btn.setText(mButtons[position].getText());
        return btn;
    }

Evertime我在屏幕上使用滚动第一行按钮与最后一行按钮混淆。 请帮忙 。我是android的新手。不知道该怎么办?我对使用contextView的知识非常有限。如果我删除if else阻止,是否可以纠正。

1 个答案:

答案 0 :(得分:0)

试试这个

 if (convertView == null) { // if it's not recycled, initialize some
                                // attributes

        btn = new Button(mContext);
}
else{
 btn = (Button) convertView;
}
        KeypadButton keypadButton = mButtons[position];

        switch(keypadButton.mCategory)
        {
        case MEMORYBUFFER:
            btn.setBackgroundResource(R.drawable.keypadmembuffer1);
            break;  
        case CLEAR:
            btn.setBackgroundResource(R.drawable.keypadclear1);
            break;  
        case NUMBER:
            btn.setBackgroundResource(R.drawable.keypad1);
            break;
        case OPERATOR:

            btn.setBackgroundResource(R.drawable.keypadop1);
            break;
        case OTHER: 
            btn.setBackgroundResource(R.drawable.keypadother1);
            break;
        case RESULT:
            btn.setWidth(8000);
            btn.setBackgroundResource(R.drawable.keypadresult1);
            break;
        case DUMMY:
            btn.setBackgroundResource(R.drawable.appvertical1);
            break;
        default:
            btn.setBackgroundResource(R.drawable.keypad1);
            break;
        }
        // Set OnClickListener of the button to mOnButtonClick
        if(keypadButton != KeypadButton.DUMMY)
            btn.setOnClickListener(mOnButtonClick);
        else
            btn.setClickable(false);
        // Set CalculatorButton enumeration as tag of the button so that we
        // will use this information from our main view to identify what to do
        btn.setTag(keypadButton);