0

我有一个带有 ImageView 项目的 GridView,我想以编程方式在某个位置选择一个项目。我以为这很容易,但是……setSelection什么都不做,为什么?

public class Test extends Activity {
  public void onCreate (final Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    GridView gridView = new GridView (this);
    gridView.setNumColumns (4);
    gridView.setHorizontalSpacing (16);
    gridView.setVerticalSpacing (16);
    setContentView (gridView);
    gridView.setAdapter (new BaseAdapter () {
      public int getCount () {
        return 12;
      }
      public Object getItem (int position) {
        return position;
      }
      public long getItemId (int position) {
        return position;
      }
      public View getView (int position, View convertView, ViewGroup parent) {
        ImageView view = (ImageView) convertView;
        if (view == null) {
          view = new ImageView (Test.this);
          view.setImageResource (R.drawable.image);
        }
        return view;
      }
    });

    gridView.setSelection (3); // ?? does nothing    
  }
}
4

1 回答 1

2

根据这篇文章,你不能在触摸模式下选择一个项目......所以你必须自己管理选择和项目突出显示:-/

于 2013-11-02T20:22:51.783 回答