Recycler VIew在卷轴上滞后

时间:2016-03-03 05:10:11

标签: android android-layout android-recyclerview

我有一个包含4个不同视图的回收者视图。页眉,页脚和2个不同的内容查看器。内容包含Instagram视图或Twitter视图(我自己的自定义视图,不像推特卡或其他任何东西)我对列表进行排序,但我注意到它什么时候没有排序,它似乎只是在开启时才会延迟twitter块(上半部分),但当我向下滚动到instagram块(下半部分)时,如果有任何滞后,我不会得到那么多。只有20-30个视图,所以它不像很多数据。我也试过加载本地图像,但这也没有帮助提高性能。

  

跳过146帧!应用程序可能在其主线程上做了太多工作。

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;
    }

}

view_social_twitter.com

<?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>

view_social_instagram.xml

<?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>

2 个答案:

答案 0 :(得分:0)

我建议你使用SysTrace来查看UI的内容。我看到的唯一有点贵的是字体,但你只是用它来表示页眉和页脚。

SysTrace info

答案 1 :(得分:0)

如果您在recyclerview中只有20个单元格,则可以尝试以下方法:

recyclerView.getRecycledViewPool().setMaxRecycledViews(VIEW_TYPE,0);

您必须注意,这可能会降低RecyclerView的性能。

告诉我它是否有帮助,谢谢=)。