CardView适配器滞后

时间:2017-01-21 09:40:56

标签: android android-cardview

我设法按照Google文档设置了一个地图列表,我稍微修改了一下,以便能够扩展卡片,即使我滚动它也会保留。我的问题是,当我滚动时,我觉得卡片的显示有滞后效应。这是一个向您展示的视频:video

有时我会收到此错误E/UncaughtException: java.lang.IllegalArgumentException: Pixel distance must be non-negative

我的CardviewAdapter

public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
    private List<Game> mDataset;
    private int rotationAngle = 0;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        private TextView textView_idGame;
        private ImageView imageView_arrow;
        private LinearLayout linearlayoutToExpands;
        private ConstraintLayout coordinatorLayoutToExpands;
        boolean isPopupVisible;
        private CardView cardView;
        TextView textView_nb_players;
        TextView textView_nom_j1, textView_nom_j2, textView_nom_j3, textView_nom_j4, textView_nom_j5;
        TextView textView_pts_j1, textView_pts_j2, textView_pts_j3, textView_pts_j4, textView_pts_j5;
        TextView textView_date;
        Button button_load;

        ViewHolder(View v) {
            super(v);
            textView_idGame = (TextView) v.findViewById(R.id.textView16);
            imageView_arrow = (ImageView) v.findViewById(R.id.item_description_game_more);
            isPopupVisible = false;
            cardView = (CardView) v.findViewById(R.id.cardView);

            textView_nb_players = (TextView) v.findViewById(R.id.textView17);

            textView_nom_j1 = (TextView) v.findViewById(R.id.textView20);
            textView_nom_j2 = (TextView) v.findViewById(R.id.textView21);
            textView_nom_j3 = (TextView) v.findViewById(R.id.textView22);
            textView_nom_j4 = (TextView) v.findViewById(R.id.textView23);
            textView_nom_j5 = (TextView) v.findViewById(R.id.textView24);

            textView_pts_j1 = (TextView) v.findViewById(R.id.textView25);
            textView_pts_j2 = (TextView) v.findViewById(R.id.textView26);
            textView_pts_j3 = (TextView) v.findViewById(R.id.textView27);
            textView_pts_j4 = (TextView) v.findViewById(R.id.textView28);
            textView_pts_j5 = (TextView) v.findViewById(R.id.textView29);

            textView_date = (TextView) v.findViewById(R.id.textView18);

            button_load = (Button) v.findViewById(R.id.button10);

            coordinatorLayoutToExpands = (ConstraintLayout) v.findViewById(R.id.viewToExpands);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    CardsViewAdapter(List<Game> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                          int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cards_resume_game, parent, false);
        // set the view's size, margins, paddings and layout parameters
        //...

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        final Game game = mDataset.get(position);
        Log.d("DEBUG","id game : "+game.getId_game());

        //setup idGame
        holder.textView_idGame.setText(String.valueOf(game.getId_game()));
        holder.textView_nb_players.setText(String.valueOf(game.getNb_joueur()));
        holder.textView_date.setText(String.valueOf(game.getDate()).substring(0,5));

        //Setup nom pour la view étendue
        holder.textView_nom_j1.setText(game.getJ1().getNom());
        holder.textView_nom_j2.setText(game.getJ2().getNom());
        holder.textView_nom_j3.setText(game.getJ3().getNom());

        if (game.getNb_joueur()>=4)
            holder.textView_nom_j4.setText(game.getJ4().getNom());
        else
            holder.textView_nom_j4.setVisibility(View.INVISIBLE);

        if (game.getNb_joueur()==5)
            holder.textView_nom_j5.setText(game.getJ5().getNom());
        else
            holder.textView_nom_j5.setVisibility(View.INVISIBLE);

        //Setup pts pour la view étendu
        holder.textView_pts_j1.setText(String.valueOf(Data.bdd.get_tot_pts_player(game.getId_game(),game.getJ1().getId())));
        holder.textView_pts_j2.setText(String.valueOf(Data.bdd.get_tot_pts_player(game.getId_game(),game.getJ2().getId())));
        holder.textView_pts_j3.setText(String.valueOf(Data.bdd.get_tot_pts_player(game.getId_game(),game.getJ3().getId())));

        if (game.getNb_joueur()>=4)
            holder.textView_pts_j4.setText(String.valueOf(Data.bdd.get_tot_pts_player(game.getId_game(),game.getJ4().getId())));
        else
            holder.textView_pts_j4.setVisibility(View.INVISIBLE);

        if (game.getNb_joueur()==5)
            holder.textView_pts_j5.setText(String.valueOf(Data.bdd.get_tot_pts_player(game.getId_game(),game.getJ5().getId())));
        else
            holder.textView_pts_j5.setVisibility(View.INVISIBLE);


        // Set the expanded or collapsed mode here
        if(game.expanded) {
            expandView(holder.coordinatorLayoutToExpands);
            holder.imageView_arrow.setRotation(180);
        }
        else {
            collapseView(holder.coordinatorLayoutToExpands);
            holder.imageView_arrow.setRotation(0);
        }


        // Now set the onClickListener like this
        holder.imageView_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Animate the imageView here
                animateImageView(holder.imageView_arrow);

                // Toggle the expanded attribute value
                if(game.expanded) game.expanded = false;
                else game.expanded = true;

                //Now call notifyDataSetChanged to make the change to effect
                refreshList(position);
            }
        });

        holder.button_load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Data.id_game=game.getId_game();
                Data.id_round=game.getMax_id_round();
                Data.nb_joueur = game.getNb_joueur();
                Data.joueur_1 = game.getJ1().getId();
                Data.joueur_2 = game.getJ2().getId();
                Data.joueur_3 = game.getJ3().getId();
                if(game.getNb_joueur()>=4)
                    Data.joueur_4 = game.getJ4().getId();
                if (game.getNb_joueur()==5)
                    Data.joueur_5=game.getJ5().getId();
                Intent intent = new Intent(holder.button_load.getContext(), ScoreTable.class);
                holder.button_load.getContext().startActivity(intent);
            }
        });
    }

    // Extra functions inside your adapter class to improve readability
    private void collapseView(View v) {
        CardsAnimationHelper.collapse(v);
    }

    private void expandView(View v) {
        CardsAnimationHelper.expand(v);
    }

    private void animateImageView(ImageView imageView) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "rotation",rotationAngle, rotationAngle + 180);
        anim.setDuration(animationDuration);
        anim.start();
        rotationAngle += 180;
        rotationAngle = rotationAngle % 360;
    }

    private long animationDuration = 500;

    private void refreshList(final int position) {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                notifyItemChanged(position);
            }
        }, animationDuration);
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

1 个答案:

答案 0 :(得分:0)

像素距离必须是非负面问题,可能是这个rec​​yclerview错误:

https://code.google.com/p/android/issues/detail?id=230295