回收器上下滚动回收器查看滞后

时间:2016-03-18 01:48:28

标签: android listview android-recyclerview

所以我在过去的一天里查看了数百个问题,阅读了所有内容,这就是我收集的内容......

一个人说添加这个:

homeRecyclerView.setHasFixedSize(true);
我试过没做什么,因为向下或向上滚动的问题是否滞后?对两者而言。

这是一个可绘制的,我正在使用Picasso(drawable因为我想在进行服务器调用之前测试视图)所以有人说可能缓存系统正在影响它,删除Picasso并且默认情况下imageview有图像但仍然滞后...

以下是家庭活动中的声明:

homeRecyclerView = (RecyclerView) findViewById(R.id.home_recycler_view);
            homeRecyclerView.setHasFixedSize(true);
  homeAdapter = new HomeAdapter(this, homeList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        homeRecyclerView.setLayoutManager(mLayoutManager);
        homeRecyclerView.setItemAnimator(new DefaultItemAnimator());

        homeRecyclerView.setAdapter(homeAdapter);

  public interface ClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

    public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private MainHome.ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainHome.ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }

这是家庭适配器:

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{

    private List<items> items;
    private Context context;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView text;
        public ImageView pic;

        public MyViewHolder(View view) {
            super(view);
           text = (TextView) view.findViewById(R.id.text);

            pic = (ImageView) view.findViewById(R.id.pic);
        }
        }

        public HomeAdapter(Context mContext, List<items> items) {
            context = mContext;
            this.items = items;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_item, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {

            Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "font1.ttf");
            Typeface myTypefaceItalic = Typeface.createFromAsset(context.getAssets(), "font2.ttf");
            Typeface myTypefaceBold = Typeface.createFromAsset(context.getAssets(), "font3.ttf");

            Item item = items.get(position);


            holder.text.setTypeface(myTypefaceBold);


Picasso.with(context).load(R.drawable.robot).into(holder.pic);
        }

        @Override
        public int getItemCount() {
            return items.size();
        }


}

任何可以指出可能导致这种滞后滚动问题或建议的人。我从适配器中的持有人的listview切换到回收者视图听到它更顺畅和更好......但滚动似乎更糟......

2 个答案:

答案 0 :(得分:5)

你的问题在于

    Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "Lato_Regular.ttf");
        Typeface myTypefaceItalic = Typeface.createFromAsset(context.getAssets(), "Lato_Italic.ttf");
        Typeface myTypefaceBold = Typeface.createFromAsset(context.getAssets(), "Lato_Bold.ttf");

每次在绑定视图中初始化它。 createFromAsset需要一些内存和处理能力。每次调用绑定视图时都会执行此操作,并且您有滚动延迟。

我建议将此代码移动到您的活动中,并将其传递给您的回收站视图构造函数。

试试这个并告诉我它是否能解决您的问题。

答案 1 :(得分:2)

我认为你的问题是每次在onBindViewHolder()上创建Typeface对象。然后在构造函数上创建并保持对它们的引用。

另外,我会在onCreateViewHolder()而不是onBindViewHolder()上附加这些字体,因为它们在回收时不需要更改。

我认为这个answer也可以帮到你。