如何在gridview中单击缩略图时显示完整大小的图像

时间:2017-10-31 11:13:11

标签: android

在我的项目中,我希望每当有人点击缩略图时,应该在用户可以缩放的活动中打开全尺寸图像。但是在传递图像的网址时面临问题(我使用了arraylist)。

下面是代码

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;


public class NoticeList_Seminar extends AppCompatActivity {

GridView gridView;
ArrayList<Notice_Activity_Seminar> list;
Notice_List_Adapter_Seminar adapter_seminar = null;

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

    gridView = (GridView) findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter_seminar = new Notice_List_Adapter_Seminar(NoticeList_Seminar.this, R.layout.noticeitemslayout_seminar, list);
    gridView.setAdapter(adapter_seminar);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //Intent intent = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            //intent.putExtra("id",i);
            //startActivity(intent);
            Toast.makeText(getApplicationContext(),"full image", Toast.LENGTH_SHORT).show();
        }
    });

    Cursor cursor = App_Activity_Seminar.sqLiteHelper_seminar.getData("SELECT * FROM NOTICE_SEMINAR");
    list.clear();
    while (cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String name = cursor.getString(1);
        String date = cursor.getString(2);
        byte[] image = cursor.getBlob(3);
        list.add(new Notice_Activity_Seminar(id, name, date, image));
    }
    adapter_seminar.notifyDataSetChanged();
}

}

FullImage活动代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import static com.example.shakshi.demo.R.id.imageView;

public class FullImageActivity extends Activity {

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

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

    // Selected image id

    int pos = i.getIntExtras("position",0);

    imageView.setImageResource(Notice_List_Adapter_Seminar.noticeList[pos]);


   /* Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
  Notice_List_Adapter_Seminar imageAdapter = new 
 Notice_List_Adapter_Seminar(FullImageActivity.this);

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

imageView.setImageResource(Notice_List_Adapter_Seminar.noticeList [位置]);

}

}

我做过的改变

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int 
        i, long l) {



            String url = String.valueOf(adapterView.getItemAtPosition(i));
            Intent intent = new Intent(NoticeList_Workshop.this, 
             FullImageActivity.class);
            intent.putExtra("url", url);
            startActivity(intent);

        }
    });
完整图像活动中的

String url = getIntent().getExtras().getString("url");


   ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
   imageView.setImageResource(Integer.parseInt(url));    

2 个答案:

答案 0 :(得分:1)

使用gridViewOnClickListenerIntent

GridView grdview = (GridView) findViewById(R.id.gridview);

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

                String url = list.get(position);
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("url", url);
                startActivity(intent)
            }
        });

SecondActivity onCreate()方法中,

String url = getIntent().getExtras().getString("url");

设置Imageview = url;

的来源

答案 1 :(得分:0)

如果您使用的是Gridview。你得到了onItemClickListener,它还为你提供了点击项目的位置。因此,在onItemClickListener中,您可以从该位置的arraylist获取图像URL并将其传递给下一个活动。

如果您正在使用Recyclerview,那么您可以使用回调模式并使用它,您可以发送与用户点击包含活动的字符串相同的URL。然后,您可以将此网址传递给下一个活动。