控制回收者视图的投掷速度

时间:2015-08-20 14:05:39

标签: android android-recyclerview

我有RecyclerView我有一张图片,一些TextView和2 ImageButton s。 我在Activity中显示了7到8行。 在Android 4.4.4及以下版本中滚动非常流畅,但它在棒棒糖中提供了一些抽搐效果。 我认为控制RecyclerView的速度可以解决问题。 我搜索了它,却一无所获。相反,我发现如何将视图带到特定位置进行投掷。

但我想控制投掷速度。可以使用摩擦力。以及如何使用摩擦力。

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视图布局的适配器。

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

5 个答案:

答案 0 :(得分:9)

RecyclerView个专门针对control the fling speed设计的@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); } 方法,您只需覆盖它即可实现您在问题中提到的摩擦/减速效果。

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

DROP DATABASE [C:TEST.MDF]

答案 1 :(得分:5)

这很适合在投掷速度上设置最大阈值:

mRecyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() {

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

        if (Math.abs(velocityY) > MAX_VELOCITY_Y) {
            velocityY = MAX_VELOCITY_Y * (int) Math.signum((double)velocityY);
            mRecyclerView.fling(velocityX, velocityY);
            return true;
        }

        return false;
    }
});

答案 2 :(得分:1)

RecyclerView非常棒,因为它是超级模块化的。您可以将自定义滚动条设置为LayoutMananger

您可以使用https://github.com/apptik/MultiView/tree/master/scrollers

示例:

RecyclerView.SmoothScroller smoothScroller = new FlexiSmoothScroller(getContext())
                            .setMillisecondsPerInchSearchingTarget(100f));
smoothScroller.setTargetPosition(targetPos);
recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);

您可以在https://github.com/apptik/MultiView/blob/master/app/src/main/java/io/apptik/multiview/ScrollersFragment.java

查看更多示例

答案 3 :(得分:0)

控制您的回收者视图的滚动速度 看到这个SO帖子: Snappy scrolling in RecyclerView

基本上你可以覆盖recyclerviews smoothScroll,或者实现你自己的onGestureListener并将它附加到recyclerView

答案 4 :(得分:0)

我有同样的问题。这是减缓猛攻的解决方案。

        val maxFling=4000
        onFlingListener = object : RecyclerView.OnFlingListener() {
            override fun onFling(velocityX: Int, velocityY: Int): Boolean {
                if (velocityY > maxFling) {
                    fling(velocityX, maxFling)
                    return true
                } else if (velocityY < -maxFling) {
                    fling(velocityX, -maxFling)
                    return true
                } else {
                    return false
                }

            }

        }