我在gridview中加载了6个图像视图,如下面的代码所示。我有2个问题:
(1)当我启动应用程序时,我需要默认选择第一个图像视图。 (2)我正在设置一个监听器,它将从gridview中的6个项目中的每个项目启动一些其他活动。当我从第二个活动返回时,我需要专注于所选项目。
我如何实现这两个要求?
非常感谢任何帮助。
// Main activity
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.d(TAG, "setOnItemClickListener() - pos:" + position);
handleItemClick(position);
}
});
//class ImageAdapter extends BaseAdapter
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(160, 120));
return imageView;
}
答案 0 :(得分:1)
试试这段代码
gridview.setSelection(position); // position = 0 or wherever you wants
希望这会对你有所帮助