Listview的可点击图片,无法打开其他活动

时间:2017-04-08 16:30:28

标签: java android android-layout listview custom-adapter

[已编辑]我想制作一个图片列表视图,点击它们时应该打开另一个活动(每个可点击的活动都有一个单独的活动)但是setOnItemClickListener不能正常工作。

这是我的主要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);
                    }
                }
        );

这是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;
    }

}

这是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;}
}

这是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>

3 个答案:

答案 0 :(得分:2)

正如CommonsWare所说:

  

您正在尝试分配74649612个字节。这相当于一个   8640 x 8640像素图像。这太大了。

你必须在drawable中使用更小尺寸的图像,因为这会分配太多内存。此外,您可以使用 Glide / Picasso 来加载Images.Glide可以有效地处理位图解码,磁盘缓存,以确保更少的内存分配并避免内存不足错误。

为了将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

}

getView()内:

<强>替换

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

。通过

 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);

要点击不同的项目打开不同的活动,请使用以下代码:

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
            }
        }); 

答案 1 :(得分:0)

只有6张图片不应该最大化你的记忆。我能看到的唯一问题就是你的图像大小。

您是否使用适当的文件大小创建了适当的可绘制文件夹?

答案 2 :(得分:0)

我认为图像正在发生。您需要使用Bitmap或任何库加载高分辨率图像。或者以简单的方式,将图像添加到可绘制的nodpi目录中。它不会缩放图像。