控制回收者视图的甩动速度 [英] control fling speed for recycler view

查看:49
本文介绍了控制回收者视图的甩动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,其中有一个图像,有几个TextView s和2个ImageButton s. 我的Activity中有7-8个这样的行要显示. 滚动在android 4.4.4及以下版本中非常平滑,但在棒棒糖中却具有一定的抽动效果. 我认为控制RecyclerView的猛击速度可以解决问题. 我进行了搜索,但一无所获.相反,我发现了如何将视图移至特定位置.

I have a RecyclerView in which i have an image ,a few TextViews and 2 ImageButtons. I have 7-8 such rows to display in my Activity. The scroll is very smooth in android 4.4.4 and below versions but it kind of gives some jerking effect in lollipop. I think that controlling the fling speed for the RecyclerView will do the trick. I searched for it, but found nothing.Instead i found how to take the view to a particular position for the fling.

但是我想控制逃跑速度.可以使用摩擦力.以及如何使用摩擦力.

But I want to control the fling speed. Can friction be used. And how to use friction.

menuRecyclerView = (RecyclerView) findViewById(R.id.menuList);
menuRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(context);
menuRecyclerView.setLayoutManager(llm);     
Scroller sc = new Scroller(context);
sc.setFriction(ViewConfiguration.getScrollFriction() * 10);

这是我用来设置rcycler视图布局的适配器.

This is my adapter to set rcycler view layout.

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import news.circle.circle.R;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.CustomViewHolder> {
    public RecyclerViewAdapter(Context context, List<MenuDescription> listOrder, DatabaseHandlerCart db
            , List<MenuDescription> vegList, List<MenuDescription> nonVegList, String menuTextData) {
        this.listOrder = listOrder;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
        imageLoader = new ImageLoader(context);
        this.db = db;
        this.vegList = vegList;
        this.nonVegList = nonVegList;
        this.completeList = listOrder;
        this.menuTextData = menuTextData;
    }

    @Override
    public RecyclerViewAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view, parent, false);
            return new CustomViewHolder(view, viewType);
        } else if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
            return new CustomViewHolder(view, viewType);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewAdapter.CustomViewHolder holder, int position) {
        if (holder.Holderid == 1) {
            final MenuDescription menu = listOrder.get(position - 1);
            holder.title.setText(menu.getName() + "");
            holder.quant.setText(menu.getQuantity() + "");
            holder.description.setText(menu.getDescription());
            holder.title.setTypeface(Fonts.getFont(context, Constants.AVENIR_REGULAR));
            holder.description.setTypeface(Fonts.getFont(context, Constants.AVENIR_REGULAR));
            Glide.with(context).load(menu.getFlag_path()).placeholder(R.drawable.ic_order_now).fitCenter().into(holder.image);
            holder.inc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Constants.internet) {
                        menu.decrQuantity();
                        if (menu.getQuantity() == 0) {
                            menu.setDecr_button_visibility(0);
                            try {
                                db.DeleteCartItem(menu.getItem_id());
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        } else {
                            try {
                                db.updateQuantity(menu.getQuantity(), menu.getItem_id());
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        holder.quant.setText(menu.getQuantity() + "");
                        notifyDataSetChanged();
                    } else {
                        Toast.makeText(context, "Please check your internet connection", Toast.LENGTH_SHORT).
                    }
                    show();
                }
            });
            holder.decr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Constants.internet) {
                        try {
                            if (!db.CheckIsItemAlreadyInDBorNot(menu.getItem_id(), "cart")) {
                                menu.incrQuantity();
                                db.addDataToCart(menu);
                                // cart_items.add(menu);

                                holder.quant.setText(menu.getQuantity() + "");
                            } else {
                                menu.incrQuantity();
                                db.updateQuantity(menu.getQuantity(), menu.getItem_id());
                                holder.quant.setText(menu.getQuantity() + "");
                            }

                        } catch (SQLException | IllegalStateException e) {
                            e.printStackTrace();
                        }


                        if (menu.getDecr_button_visibility() == 1) {
                            holder.decr.setVisibility(View.VISIBLE);
                            holder.quant.setVisibility(View.VISIBLE);
                            if (total == 1)
                                RecyclerViewMainActivity.bottomBarUp();

                            notifyDataSetChanged();
                        } else
                            Toast.makeText(context, "Please check your internet connection", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return (null != listOrder ? listOrder.size() + 1 : 0);
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }
}

推荐答案

有一种RecyclerView方法专门设计用于

There is one RecyclerView method specifically designed to control the fling speed you only need to override it to achieve the friction/slowdown effect you mentioned in your question.

您可以尝试的一个简单实现:

One simple implementation you could try:

@Override
public boolean fling(int velocityX, int velocityY)
{

     // if FLING_SPEED_FACTOR between [0, 1[ => slowdown 
     velocityY *= FLING_SPEED_FACTOR; 

     return super.fling(velocityX, velocityY);
} 

这篇关于控制回收者视图的甩动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆