0
我創建了一個帶有多個回收站視圖的標籤佈局示例應用程序。 當滾動或滑動時,製表佈局和回收站查看滯後
但在這裏,當我滾動recyclerview或在標籤之間滑動。應用程序滯後,變得非常緩慢。
第一i-用的OutOfMemoryError面對,當我施加
android:largeHeap="true"
android:hardwareAccelerated="true"
到清單,它解決了。 但我的應用程序仍然滯後。
(它仿真器的作品真的FINE)
請人幫我解決我的問題。
我的整個項目提供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;
}
}
TabFragment與RecyclerViews
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;
}
}
有多大,你的圖像? – asadmshah
700x700以下,大小小於80kb –
我猜你的圖片可能太大了。你有xxhdpi-xhdpi-hdpi-mdpi - 等嗎?如果不嘗試將圖像放在drawable-nodpi中。 – z3n105