Android - 如何选择GridView中的项目?

时间:2013-10-31 20:22:26

标签: java android gridview selection

我有一个带有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    
  }
}

1 个答案:

答案 0 :(得分:1)

根据此article,您无法在触摸模式中选择项目... 因此,您必须自己管理选择和项目突出显示: - /