gpt4 book ai didi

android - 如何在GridView中选择多张图片?

转载 作者:行者123 更新时间:2023-11-29 17:52:11 25 4
gpt4 key购买 nike

这是 GridView 主页...在 GridView 上设置自定义适配器...所有的东西都工作正常,但我想选择多张图片...

gridview=(GridView)findViewById(R.id.gridview_robot_ghost);
gridview.setAdapter(new customGridAdapter(this, GHOST_NUMBER));

gridview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText(getApplicationContext(), "Your Verification is Succeesfull "+ "\n Selected Ghost Position is "+position, Toast.LENGTH_LONG).show();

}

});

自定义网格适配器:

customGridAdapter extends BaseAdapter{
private Context context;

private final String[] grid_values;

public customGridAdapter(Context context,String[] grid_values)
{
this.context=context;
this.grid_values=grid_values;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return grid_values.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;

if(convertView==null)
{
gridView=new View(context);
gridView=inflater.inflate(R.layout.grid_item, null);
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
String ghost=grid_values[position];

if (ghost.equals("Ghost1")) {

imageView.setImageResource(R.drawable.ghost4);

} else if (ghost.equals("Ghost2")) {

imageView.setImageResource(R.drawable.ghost4);

} else if (ghost.equals("Ghost3")) {

imageView.setImageResource(R.drawable.ghost3);

} else {

imageView.setImageResource(R.drawable.ghost4);
}

} else {
gridView = (View) convertView;
}

return gridView;
}

最佳答案

OnItemClickListener 上将 View 的背景更改为所选项目并将所选项目添加到某个列表中...

如果项目已经被选中,点击特定项目改变背景为正常并从列表中移除

关于android - 如何在GridView中选择多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22094089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com