滚动或滑动时,选项卡布局和Recycler View滞后

时间:2015-12-03 06:22:38

标签: android performance android-recyclerview recycler-adapter

我创建了一个带有多个Recyclerviews标签布局的示例应用。 app Screenshot

但是在这里我滚动recyclerview或在标签之间滑动。应用程序滞后并变得非常慢。

首先我面对的是OutOfmemoryError,当我申请时

android:largeHeap="true"
android:hardwareAccelerated="true"

到清单,它解决了。 但我的应用程序仍然落后很多。

(它在仿真器中非常精细)

请任何人帮我解决我的问题。

我的整个项目在Github

中可用

RecyclerView适配器

import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;



public class VideoManager extends RecyclerView.Adapter<VideoManager.RecyclerViewHolder> {

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView mName;
        ImageView mImage,mShare,mPlay,mDownload;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mName = (TextView) itemView.findViewById(R.id.title);
            mImage = (ImageView) itemView.findViewById(R.id.image);
            mShare = (ImageView) itemView.findViewById(R.id.share);
            mPlay = (ImageView) itemView.findViewById(R.id.play);
            mDownload = (ImageView) itemView.findViewById(R.id.download);
        }
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
        return new RecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
        // get the single element from the main array

        final VideoDatabase items = VideoDatabase.VIDEO[i];
        // Set the values
        viewHolder.mName.setText(items.get(VideoDatabase.Field.NAME));
        viewHolder.mImage.setImageResource(items.geti(VideoDatabase.Field.IMAGE));
        viewHolder.mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar snackbar = Snackbar.make(v, "Streaming", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });
        viewHolder.mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar snackbar = Snackbar.make(v, "Play", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });
        viewHolder.mShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar snackbar = Snackbar.make(v, "Sharing", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });
        viewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar snackbar = Snackbar.make(v, "Downloading", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });
    }


    @Override
    public int getItemCount() {

        return VideoDatabase.VIDEO.length;
    }


}

的DataProvider

public class VideoDatabase {

    // I am going to define here the projectdatabase, public static and constant,
    // so that every class in the project will be able to read it.
    // In a sense, we just mimic the standard behaviour of a database.
    public static final VideoDatabase[] VIDEO = new VideoDatabase[]{
            new VideoDatabase("GOOD NIGHT", R.drawable.gudmorng),
            new VideoDatabase("GOOD MORNING", R.drawable.gudnyt),
            new VideoDatabase("GOOD NIGHT", R.drawable.gudnyt1),
            new VideoDatabase("GOOD NIGHT", R.drawable.gudmorng),
            new VideoDatabase("GOOD MORNING", R.drawable.gudnyt),
            new VideoDatabase("GOOD NIGHT", R.drawable.gudnyt1),
        };

    // The fields associated to the person
    private final String mVideoName;
    private final int mImage;

    VideoDatabase(String name, int image) {
        mVideoName = name;
        mImage = image;

    }

    // This method allows to get the item associated to a particular id,
    // uniquely generated by the method getId defined below
    public static VideoDatabase getItem(int id) {
        for (VideoDatabase item : VIDEO) {
            if (item.getId() == id) {
                return item;
            }
        }
        return null;
    }

    // since mName and mPhone combined are surely unique,
    // we don't need to add another id field
    public int getId() {
        return mVideoName.hashCode();
    }

    public static enum Field {
        NAME, IMAGE
    }

    public String get(Field f) {
        switch (f) {
            case NAME:
            default:
                return mVideoName;
        }
    }

    public int geti(Field f) {
        switch (f) {
            case IMAGE:

        }
        return mImage;
    }
}

使用RecyclerViews的TabFragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Tab1_videos extends Fragment {


    public Tab1_videos() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.tab1_videos, container, false);

        RecyclerView rv = (RecyclerView) v.findViewById(R.id.home_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true); // to improve performance
        rv.setAdapter(new VideoManager()); // the projectdatabase manager is assigner to the RV

        RecyclerView grid = (RecyclerView) v.findViewById(R.id.home_recyclerview2);
        LinearLayoutManager llm2 = new GridLayoutManager(getContext(),2);
        grid.setLayoutManager(llm2);
        grid.setHasFixedSize(true); // to improve performance
        grid.setAdapter(new VideoManager()); // the projectdatabase manager is assigner to the RV

        return v;
    }


}

1 个答案:

答案 0 :(得分:0)

使用任何图片加载库,例如Universal Image Loader。

这里使用UIL

`DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.mirlogo)
                .showImageForEmptyUri(R.drawable.gallery_no_image)
                .showImageOnFail(R.drawable.gallery_image_fail)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

ImageLoader.getInstance().displayImage(images, viewHolder.mImage, options);
相关问题