如何能够点击图像并加载全屏视图(使用GridView和Picasso)?

时间:2015-10-27 21:40:01

标签: android gridview picasso

我正在尝试使用Picasso使用来自在线存储库的图像创建GridView。我还希望用户能够点击图像并加载全屏以进行查看。请帮帮我!

以下是我的适配器代码:

public class FlowersAdapter extends ArrayAdapter<Flower> {
public FlowersAdapter(Context context) {
    super(context, 0);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_flower, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Flower flower = getItem(position);

    holder.getTvName().setText(flower.getName() + " " + position);

    Picasso.with(getContext())
            .load(flower.getPhotoUrl())
            .into(holder.getIvPhoto());

    return convertView;
}

private final static class ViewHolder {
    private ImageView ivPhoto;
    private TextView tvName;

    public ViewHolder(View view) {
        ivPhoto = (ImageView) view.findViewById(R.id.ivPhoto);
        tvName = (TextView) view.findViewById(R.id.tvName);
    }

    public ImageView getIvPhoto() {
        return ivPhoto;
    }

    public TextView getTvName() {
        return tvName;
    }
}

}

以下是完整图片代码:

public class FullImage extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        // get intent data
        Intent i = getIntent();

        int getPhotoUrl = i.getExtras().getInt("getPhotoUrl");

        ImageView getIvPhoto = (ImageView) findViewById(R.id.full_image_view);

        Picasso.with(this) //
                .load(getPhotoUrl) //
                .into(getIvPhoto);
    }

}

以下是活动代码:

public class SecondActivity extends AppCompatActivity {
GridView gvFlowers;
FlowersAdapter flowersAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    gvFlowers = (GridView) findViewById(R.id.gvFlowers);
    flowersAdapter = new FlowersAdapter(this);
    gvFlowers.setAdapter(flowersAdapter);
    for (int index = 0; index < 100; index++) {
        flowersAdapter.add(new Flower("http://www.bollea.com/wp-content/uploads/2013/06/blush-pink-bouquet-th.jpg", "0"));
        flowersAdapter.add(new Flower("http://www.kwiaty.lublin.pl/blog/wp-content/uploads/2013/01/bukiet-%C5%9Blubny-11-250x250.jpg", "2"));
        flowersAdapter.add(new Flower("http://litbimg8.rightinthebox.com/images/250x250/201503/qbzrsr1427266951513.jpg", "2"));
        flowersAdapter.add(new Flower("http://fs40.www.ex.ua/get/203280815/wXsJTXyBfUzVrbiuQX.jpg", "asd"));
        flowersAdapter.add(new Flower("http://www.bollea.com/wp-content/uploads/2011/07/pink-bouquet-th.jpg", "0"));
        flowersAdapter.add(new Flower("http://www.bollea.com/wp-content/uploads/2012/08/nice-bouquet-th1.jpg", "0"));
        flowersAdapter.add(new Flower("http://www.packageperfect.net/images/Product/medium/6193.jpg", "0"));
        flowersAdapter.add(new Flower("http://www.womenclub.ru/images/stories/fashion/wedding/Bridal_Bouquet_4.jpg", "0"));
        flowersAdapter.add(new Flower("http://www.bollea.com/wp-content/uploads/2013/06/blush-pink-bouquet-th.jpg", "0"));
        flowersAdapter.add(new Flower("http://www.bollea.com/wp-content/uploads/2013/06/blush-pink-bouquet-th.jpg", "0"));

    }

    gvFlowers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            // Sending image url to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImage.class);

            i.putExtra("url", position);
            startActivity(i);
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

如果我没有错。您可以向.setOnClickListener()添加ImageView。就像一个按钮。 别忘了在房产中检查它是否可点击。