简体   繁体   English

可点击图片的列表视图,无法打开其他活动

[英]Listview of clickable images, unable to open other activities

[Edited]I wanted to make a listview of images and on clicking them another activity should open(a separate activity for each clickable) but the setOnItemClickListener is not working. [编辑]我想制作图像的列表视图,单击它们时应打开另一个活动(每个可单击的对象都单独活动),但是setOnItemClickListener无法正常工作。

This is my Main Activity.java 这是我的主要Activity.java

package com.example.priyanshu.justforfun;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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


        final ArrayList<Block> blocks = new ArrayList<Block>();

        blocks.add(new Block(R.drawable.one,R.drawable.two));
        blocks.add(new Block(R.drawable.three,R.drawable.four));
        blocks.add(new Block(R.drawable.five,R.drawable.six));
        blocks.add(new Block(R.drawable.seven,R.drawable.eight));
        blocks.add(new Block(R.drawable.nine,R.drawable.ten));

        ImageAdapter adapter = new ImageAdapter(MainActivity.this, blocks, R.color.gray);

        ListView listView = (ListView) findViewById(R.id.list);

        listView.setAdapter(adapter);


        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Intent intent = new Intent(MainActivity.this,DemoActivity.class);
                        startActivity(intent);
                    }
                }
        );

This is Imageadapter.java- 这是Imageadapter.java-

public class ImageAdapter extends ArrayAdapter<Block> {

    private int mColorResourceId;

    public ImageAdapter(Activity context, ArrayList<Block> words, int colorResourceId) {
        super(context, 0, words);
        mColorResourceId = colorResourceId;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        Block currentBlock = getItem(position);

         ImageButton iconView1 = (ImageButton) listItemView.findViewById(R.id.im1);
         ImageButton iconView2 = (ImageButton) listItemView.findViewById(R.id.im2);

        iconView1.setImageResource(currentBlock.getImage1ResourceId());
        iconView2.setImageResource(currentBlock.getImage2ResourceId());

        return listItemView;
    }

}

This is Block.java- 这是Block.java-

public class Block {

    private int mImage1ResourceId = NO_IMAGE_PROVIDED;
    private int mImage2ResourceId = NO_IMAGE_PROVIDED;

    private static final int NO_IMAGE_PROVIDED = -1;

    public Block(int Image1ResourceId ,int Image2ResourceId) {
        mImage1ResourceId = Image1ResourceId;
        mImage2ResourceId = Image2ResourceId;
    }

    public int getImage1ResourceId(){  return mImage1ResourceId;}

    public int getImage2ResourceId(){  return mImage2ResourceId;}
}

This is List_item.xml 这是List_item.xml

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:id="@+id/list_item">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:src="@drawable/background_splash"
        android:id="@+id/im1"
        />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:src="@drawable/background_splash"
        android:id="@+id/im2" />
</LinearLayout>

As CommonsWare said: 正如CommonsWare所说:

You are trying to allocate 74649612 bytes. 您正在尝试分配74649612字节。 This is equivalent to a 8640 x 8640-pixel image. 这等效于8640 x 8640像素的图像。 This is much too large. 这太大了。

You have to use much smaller sized images in drawable as this allocates too much memory. 您必须在drawable中使用小得多的图像,因为这会分配过多的内存。 Moreover, You can Use Glide/Picasso to load your Images.Glide handles bitmap decoding, disk caching efficiently to ensure lesser memory allocation and avoide out of memory error. 此外,您可以使用Glide / Picasso加载图像.Glide可有效处理位图解码和磁盘缓存,以确保更少的内存分配并避免内存不足错误。

In order to use Glide to your project, First, Add this to dependency in app gradle file : 为了在您的项目中使用Glide,首先,将其添加到app gradle文件的依赖项中:

repositories {
     mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {

    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:support-v4:xx.x.x'  // According to your compileSdkVersion

}

Inside your getView() : 在您的getView()内部:

Replace: 更换:

 iconView1.setImageResource(currentBlock.getImage1ResourceId());
 iconView2.setImageResource(currentBlock.getImage2ResourceId());

With: 带有:

 Glide.with(context)
                .load(currentBlock.getImage1ResourceId())
                .placeholder(R.drawable.placeholder)   //Use low res image as placeholder
                .error(R.drawable.imagenotfound)      //Use low res image as error image
                .into(iconView1);

 Glide.with(context)
                .load(currentBlock.getImage2ResourceId())                   
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.imagenotfound)
                .into(iconView2);

To open different activities on click different items use the following code: 要打开单击不同项目的不同活动,请使用以下代码:

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

                if(position == 0){
                   Intent i = new Intent(MainActivity.this, NextActivity1.class);
                   startActivity(i);
                }
                else if(position == 1){
                   Intent i = new Intent(MainActivity.this, NextActivity2.class);
                   startActivity(i);
                }
                // Do as above for rest of the positions
            }
        }); 

Having only 6 images shouldn't max out your memory. 仅包含6张图片不应使您的内存最大。 The only thing I can see making problem here is size of your images. 我唯一能看到的问题是图像的大小。

Did you create appropriate drawable folders with proper file size? 您是否创建了具有适当文件大小的适当可绘制文件夹?

I think it's occurring for images. 我认为图像正在发生。 You need to load the high resolution image using Bitmap or any library. 您需要使用位图或任何库加载高分辨率图像。 Or for simple way, add the images to drawable nodpi directory. 或者以简单的方式,将图像添加到可绘制的nodpi目录。 It won't scale the image. 它不会缩放图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM