GridView onclick将单独的ImageView中的图像更改为单击的图像

时间:2014-08-17 12:03:40

标签: android gridview bitmap onclick imageview

我现在已经坚持了一段时间了,我知道通常会使用res / drawable文件夹并将每个R.drawable.img1放入一个Integer []数据但是在这个特定的实例中我是从由我的系统“MyDir”自动创建的文件。

我所拥有的是一个fragment_gallery.xml,左边是GridView,右边是一个单独的ImageView。在GridView中单击图像时,我希望ImageView更改为该图像。有人有什么建议吗?我已经遍布stackoverflow并且还没有人问过这个特定场景(或者至少是详细的)。

private ImageView imageView;
    // a Bitmap object
    private Bitmap bmp;
    // an array that stores the pixel values
    private int intArray[];
    private ImageGridViewAdapter imageAdapter;
    private GridView gridView;
    private String directoryName = "/MyDir/";
    private static final String GRIDVIEW_TAG = "Android Logo";

    // private LinearLayout linear;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_gallery, container,
                false);
        // assigns gridView to an object
        gridView = (GridView) rootView.findViewById(R.id.gridview);

        gridArchitecture(rootView);

        /*
         * Save image to external SD card and create new file if said file is
         * not already created
         */
        String ExternalStorageDirectoryPath = Environment
                .getExternalStorageDirectory().getAbsolutePath();

        // Name of the folder
        String targetPath = ExternalStorageDirectoryPath + directoryName;

        // Toast showing name of directory which images are saved to.
        Toast.makeText(getActivity().getApplicationContext(), targetPath,
                Toast.LENGTH_LONG).show();
        File targetDirector = new File(targetPath);

        File[] files = targetDirector.listFiles();
        for (File file : files) {
            imageAdapter.add(file.getAbsolutePath());
        }

        /**
         * GridView is waiting for an image to be selected
         */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // image number
                int num = (position + 1);

                // When image is selected show image number
                Toast.makeText(getActivity().getApplicationContext(),
                        "image : " + num, Toast.LENGTH_SHORT).show();


                imageView = (ImageView) view.findViewById(R.id.gridview);

            }

        });

        return rootView;
    }

    private void gridArchitecture(View rootView) {
        // Sets the Tag
        gridView.setTag(GRIDVIEW_TAG);

        /*
         * Adapt the image for the GridView format
         */
        imageAdapter = new ImageGridViewAdapter(getActivity()
                .getApplicationContext());
        gridView.setAdapter(imageAdapter);

        // Error - getActivity() causes an error here
        imageView = (ImageView) rootView.findViewById(R.id.imageView);

        // Set the orientation to landscape
        getActivity().setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

对此有任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

也许在onItemClick()中,您需要将带有资源的图像设置为您的图像和位置。

之类的东西
imageView.setImageURI(files[position]);

但我也是初学者在android中,我很抱歉当它错了:)

答案 1 :(得分:0)

我实际上在这里提出了问题OnClickItem() in gridView how do I show the same image in a separate imageView,答案由Tomasz Gawel提供