滚动后Gridview搞砸了

时间:2015-01-08 10:55:08

标签: android gridview scroll adapter

我正在使用GridView进行填字游戏。在滚动网格视图后,所有黑色部分(非文字框)显示不会发生的字母。

这是我的适配器。有人可以帮忙吗?感谢

private class CrossWordPuzzleAdapter extends BaseAdapter {
    private Context mContext;
    private int mWidth, mHeight;

    public CrossWordPuzzleAdapter(Context c, int width, int height) {
        mContext = c;
        mWidth = width;
        mHeight = height;
    }

    public int getCount() {
        return mWidth * mHeight;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.box_layout, null);

            holder = new ViewHolder();
            holder.numberText = (TextView) convertView.findViewById(R.id.numberText);
            holder.charText = (GridViewItem) convertView.findViewById(R.id.charText);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        int x = -1, y = -1;

        if (position < mWidth) {
            x = position;
            y = 0;
        } else {
            x = position % mWidth;
            y = position / mHeight;
        }
        if (mPuzzleBoard.getCharPlace(x, y) != '\0' && mPuzzleBoard.getCharPlace(x, y) != ' ') {
            String questionNoStr = mPuzzleBoard.getQuestionNo(x, y);

            holder.numberText.setText(questionNoStr);
            if (isRevealed) {
                holder.charText.setText("" + Character.toString(mPuzzleBoard.getCharPlace(x, y)).toUpperCase());
                holder.charText.setTextColor(Color.GREEN);
            }
            holder.charText.setBackground(getResources().getDrawable(R.drawable.letter_box_white));
        } else {
            holder.charText.setBackground(getResources().getDrawable(R.drawable.letter_box_black));
            convertView.setSelected(false);
        }

        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。显示字母的错误框实际上是之前包含字母的上一个视图框。因此,在滚动后,位置全部搞砸了,即使我已将框颜色设置为黑色,但我没有将框设置为空。

所以这就是我所做的:

            else {
                holder.numberText.setText(""); //<< reset the box to empty
                holder.charText.setText(""); //<< reset the box to empty
                holder.charText.setBackground(getResources().getDrawable(R.drawable.letter_box_black));
                convertView.setSelected(false);
            }