GridView Adapter.getView()未正确设置选定状态

时间:2012-09-22 15:53:34

标签: android button gridview adapter selected

我有一个GridView,我正在填充ArrayAdapter的自定义子类。此适配器返回我自定义为可选择的按钮(请参阅Android ImageButton with a selected state?)。这个工作到目前为止,单击按钮选择它们(使用选择器 - 背景可见)。

问题是:我无法从一开始就将这些按钮设置为选定状态。当我第一次启动视图时,它们只是显示未选中状态。

我创建了一个简单的测试项目来说明问题:

package com.example.buttonselection;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;

public class MainActivity extends Activity {

    public class SelectButtonAdapter extends ArrayAdapter<String> {

        public SelectButtonAdapter(Context context) {
            super(context, 0);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String name = getItem(position);

            View rowView = convertView;
            if (rowView == null || !(rowView instanceof Button)) {
                rowView = new Button(getContext());
                ((Button)rowView).setOnClickListener(new OnClickListener() {
                    public void onClick(View button) {
                        if (button.isSelected()){
                            button.setSelected(false);
                        } else {
                            button.setSelected(true);
                        }
                    }
                });
            }

            Button button = (Button)rowView;
            button.setText(name);
            button.setBackgroundResource(R.drawable.button_selection);

            button.setSelected(true); // this does not work

            return button;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView grid = (GridView)findViewById(R.id.gridview);

        SelectButtonAdapter adapter = new SelectButtonAdapter(this);
        adapter.add("One");
        adapter.add("Two");
        adapter.add("Three");
        grid.setAdapter(adapter);
    }
}

因此,我甚至无法恢复使用onSaveInstanceState保存的按钮的状态。我该如何解决或解决这个问题?

我很感激任何帮助!

编辑:这是我的button_selection.xml,虽然这应该没问题,因为选择按钮以后工作正常。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>

            <solid android:color="@color/bet_button_pressed" />

            <stroke
                android:width="2dip"
                android:color="@color/white" />

        </shape>
    </item>

    <item android:state_selected="true">
        <shape>

            <solid android:color="@color/bet_button_selected" />

            <stroke
                android:width="2dip"
                android:color="@color/white" />

        </shape>
    </item>

        <item>
        <shape>

            <gradient
                android:angle="90"
                android:startColor="@color/bet_button_dark_green"
                android:endColor="@color/bet_button_light_green"
                android:centerX="0.5"
                android:centerY="0.5" />

            <stroke
                android:width="2dip"
                android:color="@color/white" />

        </shape>
    </item>
</selector>

2 个答案:

答案 0 :(得分:1)

我找到了这个bug的解决方法。我只是存储外部天气按钮被选中并覆盖按钮的ondraw-method以在每次绘制时设置正确的状态。这有一个额外的好处,选择可以更容易持久。

这是解决方法(只是一个简单的概念证明,我的生产代码更复杂):

package com.example.buttonselection;

import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;

public class MainActivity extends Activity {

    public class SelectButtonAdapter extends ArrayAdapter<String> {

        public SelectButtonAdapter(Context context) {
            super(context, 0);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String name = getItem(position);

            View rowView = convertView;
            if (rowView == null || !(rowView instanceof Button)) {
                rowView = new Button(getContext()) {
                    @Override
                    protected void onDraw(Canvas canvas) {
                        setSelected(selectedButtons.contains(getText()));
                        super.onDraw(canvas);
                    }
                };
                ((Button)rowView).setOnClickListener(new OnClickListener() {
                    public void onClick(View button) {
                        String text = ((Button)button).getText().toString();

                        if(selectedButtons.contains(text)) {
                            selectedButtons.remove(text);
                        } else {
                            selectedButtons.add(text);
                        }
                        button.invalidate();
                    }
                });
            }

            Button button = (Button)rowView;
            button.setText(name);
            button.setBackgroundResource(R.drawable.button_selection);

            return button;
        }

    }
    private Set<String> selectedButtons = new HashSet<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView grid = (GridView)findViewById(R.id.gridview);

        SelectButtonAdapter adapter = new SelectButtonAdapter(this);
        adapter.add("One");
        adapter.add("Two");
        adapter.add("Three");
        grid.setAdapter(adapter);

        // this selects the first button from the start
        selectedButtons.add("One");
    }

}

旁注: 有人会认为,版本4.1 API不会包含这样明显的错误。浪费在这上面的时间令人沮丧,并没有激励为这个系统开发更多。

答案 1 :(得分:1)

我遇到同样的问题。作为一种解决方法,我使用runnable来设置要选择的视图。

适用于您的情况:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           String name = getItem(position);

           View rowView = convertView;
           if (rowView == null || !(rowView instanceof Button)) {
               rowView = new Button(getContext());
               ((Button)rowView).setOnClickListener(new OnClickListener() {
                    public void onClick(View button) {
                        if (button.isSelected()){
                            button.setSelected(false);
                        } else {
                            button.setSelected(true);
                        }
                   }
               });
            }

            Button button = (Button)rowView;
            button.setText(name);
            button.setBackgroundResource(R.drawable.button_selection);

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    button.setSelected(true);
                }
            }, 50);

            return button;
        }

50只是我选择使用的随机数,您可以根据需要进行更改。

这个解决方法适合我,我个人更喜欢这个,而不是覆盖onDraw方法。

希望这有助于某人。