使用RecyclerView

时间:2015-10-27 06:14:39

标签: out-of-memory android-recyclerview

我正在使用recyclerview创建一个应用程序。虽然我尝试在移动设备上滚动它但是我收到以下错误。

  

10-27 11:26:50.635 30536-30536 / com.example.sabudaniel61.ktdc   E / AndroidRuntime:致命异常:主要           java.lang.OutOfMemoryError                   在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                   在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)                   在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)                   在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)                   在android.content.res.Resources.loadDrawable(Resources.java:2854)                   在android.content.res.Resources.getDrawable(Resources.java:1521)                   在android.widget.ImageView.resolveUri(ImageView.java:628)                   在android.widget.ImageView.setImageResource(ImageView.java:361)                   在com.example.sabudaniel61.ktdc.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:41)                   在com.example.sabudaniel61.ktdc.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:15)                   在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:4402)                   在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:3717)                   在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:3609)                   在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1859)                   在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311)                   在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274)                   在android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1102)                   在android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:959)                   在android.support.v7.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:3062)                   在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:791)                   在android.view.Choreographer.doCallbacks(Choreographer.java:591)                   在android.view.Choreographer.doFrame(Choreographer.java:560)                   在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:777)                   在android.os.Handler.handleCallback(Handler.java:725)                   在android.os.Handler.dispatchMessage(Handler.java:92)                   在android.os.Looper.loop(Looper.java:176)                   在android.app.ActivityThread.main(ActivityThread.java:5302)                   at java.lang.reflect.Method.invokeNative(Native Method)                   在java.lang.reflect.Method.invoke(Method.java:511)                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)                   在dalvik.system.NativeStart.main(本地方法)

以下是recyclerview的布局:

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relative1">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="480dp"
          android:scaleType="fitXY"
            android:id="@+id/imageView1"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:id="@+id/textView1"

                android:layout_centerHorizontal="true"
                android:layout_below="@+id/imageView1"
                android:textSize="35sp"
                android:textColor="#263238"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/textView2"

                android:layout_centerHorizontal="true"
                android:layout_below="@+id/textView1"
                android:textSize="20sp"
                android:textColor="#263238"/>
    </RelativeLayout>

RecyclerView的课程:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Sabudaniel61 on 2015-10-15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    HotelData[] hoteldata;public int count;View v1;MainActivity main;

    View view;
    RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
        this.hoteldata = hoteldata;

    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
        viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
        viewHolder.textView1.setText(hoteldata[i].getHotel());
        viewHolder.textView2.setText(hoteldata[i].getPlace());



    }

    @Override
    public int getItemCount() {
        return hoteldata.length;

    }

    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView textView1, textView2; RecyclerView recyclerView;
        ImageView imageView;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            v1=itemLayoutView;
            textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

            imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
            count=getAdapterPosition();
            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MascotFragment mascotFragment = new MascotFragment();

                    FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
                    layout.removeAllViews();
                    FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame, mascotFragment).commit();
                }
            });

        }
//        public void clicks(){
//
//        v1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                 count = getAdapterPosition();
//                if (count == 1) {
//                    Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
//                }
//
//            }});
//
//
//
//}

    }
    }

编辑代码:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
 * Created by Sabudaniel61 on 2015-10-15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    HotelData[] hoteldata;public int count;View v1;MainActivity main;

    View view;
    RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
        this.hoteldata = hoteldata;

    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
        viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
        viewHolder.textView1.setText(hoteldata[i].getHotel());
        viewHolder.textView2.setText(hoteldata[i].getPlace());



    }

    @Override
    public int getItemCount() {
        return hoteldata.length;

    }

    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView textView1, textView2; RecyclerView recyclerView;
        ImageView imageView;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            v1=itemLayoutView;
            textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
            textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

            imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
            Picasso.with(main).load(R.drawable.aryahomepage).into(imageView);
            Picasso.with(main).load(R.drawable.bolgattyhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.mascothomepage).into(imageView);
            Picasso.with(main).load(R.drawable.waterhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.samudrahomepage).into(imageView);
            Picasso.with(main).load(R.drawable.goldenhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.rainhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.periyarhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.pepperhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.nandanamhomepage).into(imageView);
            Picasso.with(main).load(R.drawable.tamarindhomepage).into(imageView);












            count=getAdapterPosition();
            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MascotFragment mascotFragment = new MascotFragment();

                    FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
                    layout.removeAllViews();
                    FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame, mascotFragment).commit();
                }
            });

        }
//        public void clicks(){
//
//        v1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                 count = getAdapterPosition();
//                if (count == 1) {
//                    Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
//                }
//
//            }});
//
//
//
//}

    }
    }

1 个答案:

答案 0 :(得分:1)

尝试使用Picasso

从资源可绘制文件夹加载图片

可以使用以下代码完成此操作:

Picasso.with(context).load(R.drawable.drawableName).into(imageView);