在您滚动微调器之前,Android Spinner Radiobutton视图不会响应

时间:2013-11-04 17:15:51

标签: android radio-button spinner

我在一个屏幕上有一个Spinner,设置如下:

    defScreenSpinner = (Spinner) v.findViewById(R.id.default_screen);
adapter =
  new SimpleAdapter(v.getContext(), data, R.layout.image_spinner_item, new String[] { "image" },
    new int[] { R.id.image }) {
    private View[] views = new View[data.size()];

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
      Log.d(TAG, "getDropDownView: " + position);
      View v = views[position];

      if (v == null) {
        // Need unique views so Radio buttons can be tracked individually
        v = inflater.inflate(R.layout.image_spinner_item, null);

        Map<String, Object> map = data.get(position);
        int imgresid = (Integer) map.get("image");
        boolean selected = (position == defScreenIdx);
        Log.d(TAG,
              String.format("populateView: position=%d, resid=%d, sel=%d", position, imgresid, (selected ? 1 : 0)));
        ImageView iv = (ImageView) v.findViewById(R.id.image);
        iv.setImageResource(imgresid);
        RadioButton cb = (RadioButton) v.findViewById(R.id.selectedimg);
        cb.setChecked(selected);
        cb.setTag(position);
        if (selected) {
          curButton = cb;
        }
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG, "Button Clicked: " + isChecked);
            if (buttonView.isChecked()) {
              onSelDefault(buttonView);
            }
          }
        });

        v.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            Log.d(TAG, "View Clicked: ");
          }
        });

        views[position] = v;
      }

      return v;
    }
  };
defScreenSpinner.setAdapter(adapter);
defScreenSpinner.setBackgroundResource(imgIds.get(defScreenIdx));
defScreenSpinner.setSelection(defScreenIdx);

...

  public void onSelDefault(View v) {
    if (curButton != null) {
      curButton.setChecked(false);
      curButton.invalidate();
    }
    curButton = (RadioButton) v;
    curButton.setChecked(true);
    defScreenIdx = (Integer) v.getTag();
    Log.d(TAG, "defIdx=" + defScreenIdx);
    defScreenSpinner.setBackgroundResource(imgIds.get(defScreenIdx));
    defScreenSpinner.setSelection(defScreenIdx);
    return;
    }

Spinner按钮看起来很好,当您在Spinner面板中单击时会出现。 问题是单选按钮根本不起作用 - 直到您滚动视图。 当我第一次点击Spinner按钮时,日志会显示:

11-04 08:42:04.831: D/PreferencesActivity(6236): getDropDownView: 0
11-04 08:42:04.831: D/PreferencesActivity(6236): populateView: position=0, resid=2130837697, sel=1
11-04 08:42:04.831: D/PreferencesActivity(6236): getDropDownView: 1
11-04 08:42:04.841: D/PreferencesActivity(6236): populateView: position=1, resid=2130837715, sel=0
11-04 08:42:04.841: D/PreferencesActivity(6236): getDropDownView: 2
11-04 08:42:04.841: D/PreferencesActivity(6236): populateView: position=2, resid=2130837725, sel=0
11-04 08:42:04.841: D/PreferencesActivity(6236): getDropDownView: 3
11-04 08:42:04.851: D/PreferencesActivity(6236): populateView: position=3, resid=2130837640, sel=0
11-04 08:42:04.851: D/PreferencesActivity(6236): getDropDownView: 4
11-04 08:42:04.851: D/PreferencesActivity(6236): populateView: position=4, resid=2130837721, sel=0
11-04 08:42:04.851: D/PreferencesActivity(6236): getDropDownView: 5
11-04 08:42:04.862: D/PreferencesActivity(6236): populateView: position=5, resid=2130837513, sel=0
11-04 08:42:04.862: D/PreferencesActivity(6236): getDropDownView: 6
11-04 08:42:04.862: D/PreferencesActivity(6236): populateView: position=6, resid=2130837507, sel=0
11-04 08:42:04.881: D/PreferencesActivity(6236): getDropDownView: 0
11-04 08:42:04.881: D/PreferencesActivity(6236): getDropDownView: 1
11-04 08:42:04.881: D/PreferencesActivity(6236): getDropDownView: 2
11-04 08:42:04.881: D/PreferencesActivity(6236): getDropDownView: 3
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 4
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 5
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 6
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 0
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 1
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 2
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 3
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 4
11-04 08:42:04.891: D/PreferencesActivity(6236): getDropDownView: 5
11-04 08:42:04.901: D/PreferencesActivity(6236): getDropDownView: 0
11-04 08:42:04.901: D/PreferencesActivity(6236): getDropDownView: 1
11-04 08:42:04.901: D/PreferencesActivity(6236): getDropDownView: 2
11-04 08:42:04.901: D/PreferencesActivity(6236): getDropDownView: 3
11-04 08:42:04.911: D/PreferencesActivity(6236): getDropDownView: 4
11-04 08:42:04.911: D/PreferencesActivity(6236): getDropDownView: 5
11-04 08:42:04.911: D/PreferencesActivity(6236): getDropDownView: 6
11-04 08:42:04.921: D/PreferencesActivity(6236): getDropDownView: 0
11-04 08:42:04.921: D/PreferencesActivity(6236): getDropDownView: 1
11-04 08:42:04.921: D/PreferencesActivity(6236): getDropDownView: 2
11-04 08:42:04.931: D/PreferencesActivity(6236): getDropDownView: 3
11-04 08:42:04.931: D/PreferencesActivity(6236): getDropDownView: 4
11-04 08:42:04.931: D/PreferencesActivity(6236): getDropDownView: 5
11-04 08:42:04.931: D/PreferencesActivity(6236): getDropDownView: 6
11-04 08:42:13.646: D/PreferencesActivity(6236): getDropDownView: 6

因此,它构建前7个单选按钮,并选择第一个。 然后我点击按钮2到4.NOTHING显示在日志中。 接下来,我将视图一直向下滚动并返回到顶部。 日志显示:

11-04 09:09:52.662: D/PreferencesActivity(6262): getDropDownView: 6
11-04 09:09:52.662: D/PreferencesActivity(6262): getDropDownView: 7
11-04 09:09:52.671: D/PreferencesActivity(6262): populateView: position=7, resid=2130837707, sel=0
11-04 09:09:52.701: D/PreferencesActivity(6262): getDropDownView: 8
11-04 09:09:52.701: D/PreferencesActivity(6262): populateView: position=8, resid=2130837709, sel=0
11-04 09:09:52.701: D/PreferencesActivity(6262): getDropDownView: 9
11-04 09:09:52.721: D/PreferencesActivity(6262): populateView: position=9, resid=2130837653, sel=0
11-04 09:09:52.721: D/PreferencesActivity(6262): getDropDownView: 10
11-04 09:09:52.721: D/PreferencesActivity(6262): populateView: position=10, resid=2130837705, sel=0
11-04 09:09:52.721: D/PreferencesActivity(6262): getDropDownView: 11
11-04 09:09:52.732: D/PreferencesActivity(6262): populateView: position=11, resid=2130837651, sel=0
11-04 09:09:52.912: D/PreferencesActivity(6262): getDropDownView: 12
11-04 09:09:52.912: D/PreferencesActivity(6262): populateView: position=12, resid=2130837514, sel=0
11-04 09:09:52.932: D/PreferencesActivity(6262): getDropDownView: 13
11-04 09:09:52.932: D/PreferencesActivity(6262): populateView: position=13, resid=2130837677, sel=0
11-04 09:09:52.942: D/PreferencesActivity(6262): getDropDownView: 14
11-04 09:09:52.961: D/PreferencesActivity(6262): populateView: position=14, resid=2130837717, sel=0
11-04 09:09:53.042: D/PreferencesActivity(6262): getDropDownView: 15
11-04 09:09:53.042: D/PreferencesActivity(6262): populateView: position=15, resid=2130837593, sel=0
11-04 09:09:53.072: D/PreferencesActivity(6262): getDropDownView: 16
11-04 09:09:53.072: D/PreferencesActivity(6262): populateView: position=16, resid=2130837689, sel=0
11-04 09:09:53.082: D/PreferencesActivity(6262): getDropDownView: 17
11-04 09:09:53.092: D/PreferencesActivity(6262): populateView: position=17, resid=2130837712, sel=0
11-04 09:09:53.092: D/PreferencesActivity(6262): getDropDownView: 13
11-04 09:09:53.092: D/PreferencesActivity(6262): getDropDownView: 12
11-04 09:09:54.762: D/PreferencesActivity(6262): getDropDownView: 11
11-04 09:09:54.802: D/PreferencesActivity(6262): getDropDownView: 10
11-04 09:09:54.862: D/PreferencesActivity(6262): getDropDownView: 9
11-04 09:09:54.862: D/PreferencesActivity(6262): getDropDownView: 8
11-04 09:09:54.872: D/PreferencesActivity(6262): getDropDownView: 7
11-04 09:09:54.952: D/PreferencesActivity(6262): getDropDownView: 6
11-04 09:09:54.952: D/PreferencesActivity(6262): getDropDownView: 5
11-04 09:09:54.952: D/PreferencesActivity(6262): getDropDownView: 4
11-04 09:09:55.092: D/PreferencesActivity(6262): getDropDownView: 3
11-04 09:09:55.092: D/PreferencesActivity(6262): getDropDownView: 2
11-04 09:09:55.111: D/PreferencesActivity(6262): getDropDownView: 1
11-04 09:09:55.111: D/PreferencesActivity(6262): getDropDownView: 0
11-04 09:09:55.111: D/PreferencesActivity(6262): getDropDownView: 5

单击第二个按钮会导致这些日志条目:(看来我之前的点击都已排队并等待某些事情......)

11-04 09:10:56.248: D/PreferencesActivity(6262): Button Clicked: true
11-04 09:10:56.248: D/PreferencesActivity(6262): Button Clicked: false
11-04 09:10:56.252: D/PreferencesActivity(6262): defIdx=1
11-04 09:10:56.325: D/PreferencesActivity(6262): Button Clicked: true
11-04 09:10:56.325: D/PreferencesActivity(6262): Button Clicked: false
11-04 09:10:56.325: D/PreferencesActivity(6262): defIdx=2
11-04 09:10:56.332: D/PreferencesActivity(6262): Button Clicked: true
11-04 09:10:56.332: D/PreferencesActivity(6262): Button Clicked: false
11-04 09:10:56.332: D/PreferencesActivity(6262): defIdx=3
11-04 09:10:56.352: D/PreferencesActivity(6262): Button Clicked: true
11-04 09:10:56.352: D/PreferencesActivity(6262): Button Clicked: false
11-04 09:10:56.352: D/PreferencesActivity(6262): defIdx=4

现在选择了第5个按钮(idx = 4)。

从现在开始,任何按钮上的任何点击都可以正常工作。选择该按钮,取消选择旧按钮。

我如何使这项工作?

1 个答案:

答案 0 :(得分:1)

您正在缓存View。不要那样做。微调器回收View本身,因此你对它们的缓存会产生冲突。您需要查看Android中常用的ViewHolder模式,并了解如何正确使用回收的ViewconvertView参数)。