Android 回收器查看卷轴上的垃圾
我有一个recycler视图,其中包含4个不同的视图持有者。一个页眉、一个页脚和两个不同的内容查看器。内容包含Instagram视图或Twitter视图(我自己的自定义VIE,不喜欢Twitter卡片或任何东西),我正在对列表进行排序,但我注意到,当它未排序时,似乎只有在Twitter块(上半部分)上才会延迟,但当我向下滚动到Instagram块(下半部分)时,如果有延迟,我不会得到那么多。只有20-30个视图,所以不像很多数据。我也尝试过只加载本地图像,但这也无助于性能 跳过146帧!应用程序可能在其主线程上做了太多工作Android 回收器查看卷轴上的垃圾,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我有一个recycler视图,其中包含4个不同的视图持有者。一个页眉、一个页脚和两个不同的内容查看器。内容包含Instagram视图或Twitter视图(我自己的自定义VIE,不喜欢Twitter卡片或任何东西),我正在对列表进行排序,但我注意到,当它未排序时,似乎只有在Twitter块(上半部分)上才会延迟,但当我向下滚动到Instagram块(下半部分)时,如果有延迟,我不会得到那么多。只有20-30个视图,所以不像很多数据。我也尝试过只加载本地图像,但这也无助于性能 跳过146帧!应用程序
公共类SocialFeedAdapter扩展了RecyclerView.Adapter{
私有静态最终int TOP\u VIEW\u TYPE=0;
私有静态最终整数底部视图类型=1;
私有静态final int TWITTER_VIEW_TYPE=2;
私有静态最终int IG_视图_类型=3;
私有最终上下文mContext;
私人最终名单;
私人停车场;
私有myapplicationmapp;
公共SocialFeedAdapter(上下文c,列表f){
mContext=c;
mFeed=f;
mInflater=LayoutInflater.from(this.mContext);
mApp=新的MyApplication();
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图;
开关(视图类型){
箱顶视图类型:
视图=最小平坦。充气(R.layout.view\u social\u top,parent,false);
返回新的TopViewHolder(视图);
箱底视图类型:
视图=最小平坦度。充气(R.layout.view\u social\u bottom,parent,false);
返回新的底部视图支架(视图);
案例视图类型:
view=mInflater.inflate(R.layout.view\u social\u twitter,parent,false);
返回新的TwitterViewHolder(视图);
案例IG_视图_类型:
view=mInflater.flate(R.layout.view\u social\u instagram,parent,false);
返回新InstagramView文件夹(视图);
违约:
view=mInflater.inflate(R.layout.view\u social\u twitter,parent,false);
返回新的TwitterViewHolder(视图);
}
}
@凌驾
public int getItemViewType(int位置){
如果(位置==0){
返回顶部视图类型;
}else if(位置==(mFeed.size()+1)){
返回底部视图类型;
}否则{
if(mFeed.get(position-1.getType()==Social.TWITTER){
返回TWITTER\u VIEW\u类型;
}否则{
返回IG_视图_类型;
}
}
}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder,int位置){
if(TopViewHolder的支架实例){
(TopViewHolder)holder.title.setTypeface(TypefaceUtil.get(mContext,TypefaceUtil.SOFIA_LIGHT));
}else if(TwitterViewHolder的持有者实例){
社会s=mFeed.get(位置-1);
如果(s.getImg()!=null){
毕加索(McContext)
.load(R.drawable.ic_twitter_黑色)
//.load(s.getImg())
.resize(s.getWidth(),s.getHeight())
.占位符(R.color.event\u background\u color)
.centerCrop()
.into(((TwitterViewHolder)holder.img);
}否则{
((TwitterViewHolder)holder.img.setVisibility(View.GONE);
}
((TwitterViewHolder)holder.username.setText(s.getUsername());
((TwitterViewHolder)holder.desc.setText(s.getBody());
}else if(InstagramView文件夹的持有人实例){
社会s=mFeed.get(位置-1);
如果(s.getImg()!=null){
毕加索(McContext)
.load(R.drawable.ic_twitter_黑色)
//.load(s.getImg())
.resize(s.getWidth(),s.getHeight())
.占位符(R.color.event\u background\u color)
.centerCrop()
.插入(((InstagramViewHolder)支架).img;
}
}else if(底部视图支架的支架实例){
((BottomViewHolder)holder.callToAction.setTypeface(TypefaceUtil.get(mContext,TypefaceUtil.SOFIA_LIGHT));
}
}
@凌驾
public int getItemCount(){
返回mFeed.size()+2;
}
类TopViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图标题;
公共TopViewHolder(查看项目视图){
超级(项目视图);
title=(TextView)itemView.findViewById(R.id.object\u stories\u header);
}
}
类BottomViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图调用操作;
公共底部视图支架(视图项视图){
超级(项目视图);
callToAction=(TextView)itemView.findViewById(R.id.follow\u call\u to\u action);
}
}
类TwitterViewHolder扩展了RecyclerView.ViewHolder{
TextView desc,用户名;
图像视图img;
公共TwitterViewHolder(查看项目视图){
超级(项目视图);
desc=(TextView)itemView.findViewById(R.id.content);
username=(TextView)itemView.findViewById(R.id.username);
img=(ImageView)itemView.findViewById(R.id.image);
}
}
InstagramViewHolder类扩展了RecyclerView.ViewHolder{
图像视图img;
公共InstagramView文件夹(查看项目视图){
超级(项目视图);
img=(ImageView)itemView.findViewById(R.id.image);
}
}
}
公共类SocialFeedFragment扩展了片段{
私有OnFragmentInteractionListener mListener;
私有上下文;
私人活动能力;
私人名单;
@垃圾箱
public class SocialFeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TOP_VIEW_TYPE = 0;
private static final int BOTTOM_VIEW_TYPE = 1;
private static final int TWITTER_VIEW_TYPE = 2;
private static final int IG_VIEW_TYPE = 3;
private final Context mContext;
private final List<Social> mFeed;
private LayoutInflater mInflater;
private MyApplication mApp;
public SocialFeedAdapter(Context c, List<Social> f){
mContext = c;
mFeed = f;
mInflater = LayoutInflater.from(this.mContext);
mApp = new MyApplication();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case TOP_VIEW_TYPE:
view = mInflater.inflate(R.layout.view_social_top, parent, false);
return new TopViewHolder(view);
case BOTTOM_VIEW_TYPE:
view = mInflater.inflate(R.layout.view_social_bottom, parent, false);
return new BottomViewHolder(view);
case TWITTER_VIEW_TYPE:
view = mInflater.inflate(R.layout.view_social_twitter, parent, false);
return new TwitterViewHolder(view);
case IG_VIEW_TYPE:
view = mInflater.inflate(R.layout.view_social_instagram, parent, false);
return new InstagramViewHolder(view);
default:
view = mInflater.inflate(R.layout.view_social_twitter, parent, false);
return new TwitterViewHolder(view);
}
}
@Override
public int getItemViewType(int position) {
if(position == 0){
return TOP_VIEW_TYPE;
}else if(position == (mFeed.size() + 1)){
return BOTTOM_VIEW_TYPE;
}else{
if(mFeed.get(position - 1).getType() == Social.TWITTER){
return TWITTER_VIEW_TYPE;
}else{
return IG_VIEW_TYPE;
}
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof TopViewHolder){
((TopViewHolder) holder).title.setTypeface(TypefaceUtil.get(mContext, TypefaceUtil.SOFIA_LIGHT));
}else if(holder instanceof TwitterViewHolder){
Social s = mFeed.get(position - 1);
if(s.getImg() != null) {
Picasso.with(mContext)
.load(R.drawable.ic_twitter_black)
//.load(s.getImg())
.resize(s.getWidth(), s.getHeight())
.placeholder(R.color.event_background_color)
.centerCrop()
.into(((TwitterViewHolder) holder).img);
}else{
((TwitterViewHolder) holder).img.setVisibility(View.GONE);
}
((TwitterViewHolder) holder).username.setText(s.getUsername());
((TwitterViewHolder) holder).desc.setText(s.getBody());
}else if(holder instanceof InstagramViewHolder){
Social s = mFeed.get(position - 1);
if(s.getImg() != null) {
Picasso.with(mContext)
.load(R.drawable.ic_twitter_black)
//.load(s.getImg())
.resize(s.getWidth(), s.getHeight())
.placeholder(R.color.event_background_color)
.centerCrop()
.into(((InstagramViewHolder) holder).img);
}
}else if(holder instanceof BottomViewHolder){
((BottomViewHolder) holder).callToAction.setTypeface(TypefaceUtil.get(mContext, TypefaceUtil.SOFIA_LIGHT));
}
}
@Override
public int getItemCount() {
return mFeed.size() + 2;
}
class TopViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TopViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.object_stories_header);
}
}
class BottomViewHolder extends RecyclerView.ViewHolder {
public TextView callToAction;
public BottomViewHolder(View itemView) {
super(itemView);
callToAction = (TextView) itemView.findViewById(R.id.follow_call_to_action);
}
}
class TwitterViewHolder extends RecyclerView.ViewHolder {
TextView desc, username;
ImageView img;
public TwitterViewHolder(View itemView) {
super(itemView);
desc = (TextView) itemView.findViewById(R.id.content);
username = (TextView) itemView.findViewById(R.id.username);
img = (ImageView) itemView.findViewById(R.id.image);
}
}
class InstagramViewHolder extends RecyclerView.ViewHolder {
ImageView img;
public InstagramViewHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.image);
}
}
}
public class SocialFeedFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private Context mContext;
private MainActivity mActivity;
private List<Social> mSocialList;
@Bind(R.id.social_feed)
RecyclerView mSocialFeed;
public SocialFeedFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (MainActivity) getActivity();
mContext = mActivity.getApplicationContext();
mSocialList = SocialFactory.generateSocialFeed(mActivity.getFeed());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_feed, container, false);
ButterKnife.bind(this, v);
mSocialFeed.setLayoutManager(new LinearLayoutManager(mContext));
mSocialFeed.setAdapter(new SocialFeedAdapter(mContext, mSocialList));
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/margin_normal"
android:background="@color/event_background_color"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/margin_small"
android:layout_marginBottom="@dimen/margin_small"
android:gravity="center"
android:textColor="@color/font_black"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:src="@drawable/ic_twitter_black"
/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="@color/font_black"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/instagram"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:padding="@dimen/margin_small"
android:layout_alignParentBottom="true"
android:src="@drawable/instagram_logo"
android:background="@color/font_black"
/>
</RelativeLayout>
</LinearLayout>
recyclerView.getRecycledViewPool().setMaxRecycledViews(VIEW_TYPE,0);