RecyclerView图像加载缓慢

时间:2015-11-02 08:34:54

标签: android recycler-adapter

我正在开发一个应用程序,我使用RecyclerView,其中我有一个图像和两个文本。有11个这样的项目。我用于加载图像。但是当我运行应用程序时,一些图像不加载。当我向上滚动然后向下滚动显示的图像。我可以做任何事情来加速图像的加载。这是我的recyclerviewcode。

RecyclerAdapter:

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());
//        Bitmap bitmap= BitmapFactory.decodeResource(Resources.getSystem(),hoteldata[i].getImageUrl());
//        Bitmap bMapScaled = Bitmap.createScaledBitmap(bitmap, 150, 100, true);
//        viewHolder.imageView.setImageBitmap(bMapScaled);
        Picasso.with(main).load(hoteldata[i].getImageUrl()).into(viewHolder.imageView);


        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;

        @Override
        public String toString() {
            return super.toString();
        }

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

    }
    }

代码编辑: 我最近添加了一个VideoView。现在当我从第4个位置点击项目时,我得到以下异常。

错误:

android.view.InflateException: Binary XML file line #690: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:619)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.example.sabudaniel61.ktdc.MascotFragment.onCreateView(MascotFragment.java:20)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5302)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:593)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.example.sabudaniel61.ktdc.MascotFragment.onCreateView(MascotFragment.java:20)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5302)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
            at android.content.res.Resources.loadDrawable(Resources.java:2854)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
            at android.widget.ImageView.<init>(ImageView.java:126)
            at android.widget.ImageView.<init>(ImageView.java:116)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:593)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.example.sabudaniel61.ktdc.MascotFragment.onCreateView(MascotFragment.java:20)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5302)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案