滚动时回收者列表卡住了

时间:2019-04-03 07:15:01

标签: android android-recyclerview

我已经添加了简单的v7.widget.RecyclerView并简单地设置了适配器,但是在滚动列表时会突然分配,滚动无法顺利进行。实际上,我也很少使用库来快速滚动,但是没有任何效果。我在适配器内有毕加索,可在“ onBindViewHolder”中完成数据设置或处理可见性时加载图像。 如果有人有更好的解决方案而不是好心帮助我。

我已经尝试了以下所有方法,但是它们都不起作用

@BindView(R.id.rv_list)
RecyclerView rvList;

ViewCompat.setNestedScrollingEnabled(rvList, false);

rvList.setNestedScrollingEnabled(false);
rvList.setHasFixedSize(true);
rvList.setItemViewCacheSize(20);
rvList.setDrawingCacheEnabled(true);                     
rvList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

以下是我的适配器代码:

            public class RecyclerAdapter extends RecyclerView.Adapter<cadapter.RecyclerAdapter.MyViewHolder> {

                Context ctx;
                ArrayList<Model> Modelslist;

                GlobalClass gc;

                View itemView;
                View parentView;
                private RecyclerAdapter.RecyclerAdapterListener listener;
                PrefManager prefManager;

                public RecyclerAdapter(Context context, ArrayList<Model> Modelslist, RecyclerAdapter.RecyclerAdapterListener listener) {

                    this.Modelslist = Modelslist;
                    this.ctx = context;
                    gc = GlobalClass.getInstance();
                    this.listener = listener;
                    prefManager = new PrefManager(context);
                }

                @Override
                public int getItemViewType(int position) {
                    return position;
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }


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

                    if (itemView == null) {
                        parentView = new RelativeLayout(this.ctx);
                    } else {
                        parentView = (RelativeLayout) itemView;
                    }
                    return new RecyclerAdapter.MyViewHolder(itemView);
                }

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

                    final Model cat = this.Modelslist.get(position);

                    holder.txtNumber.setText(cat.getNumber());
                    holder.txtName.setText(cat.getName());

                    final String flagPath = cat.getFlag();

                    if(cat.getForwarding1().equals("0")) {
                        holder.callForwardingSwitch.setChecked(true);


                        holder.txtCallRateVal.setText(cat.getForwardingCost());

                        holder.txtCallRateVal.setVisibility(View.VISIBLE);
                        holder.txtCallRate.setVisibility(View.VISIBLE);
                        holder.txtCallRateVal2.setVisibility(View.VISIBLE);
                        holder.txtCallRateVal2.setTextColor(ctx.getResources().getColor(R.color.text_grey));

                        ////VISIBLE call Forwading Text
                        holder.txtCallRateVal.setVisibility(View.VISIBLE);
                        holder.txtCallRate.setVisibility(View.VISIBLE);
                    }else{
                        holder.callForwardingSwitch.setChecked(false);

                        ////Hide call Forwading Text
                        holder.txtCallRateVal.setVisibility(View.INVISIBLE);
                        holder.txtCallRate.setVisibility(View.INVISIBLE);
                        holder.txtCallRateVal2.setVisibility(View.INVISIBLE);
                    }

                    holder.txtNumberDesc.setText(cat.getDetails());
                    holder.txtRenewDate.setText(cat.getrenewal_date());
                    holder.txtSetupVal.setText(cat.getcost());
                    holder.txtMonthlyVal.setText(cat.getmonthlycost());

                    String application_status= cat.getapplication_status();
                    System.out.println("Application status:::::: "+ application_status+" number: "+cat.getNumber());
                    if(application_status.equals("0")){
                        holder.txtStatus.setText("* Pending Approval");
                        holder.txtStatus.setVisibility(View.VISIBLE);

                        holder.txtStatus.setTextColor(ctx.getResources().getColor(R.color.account_error));

                        holder.txtCallRateVal.setVisibility(View.INVISIBLE);
                        holder.txtCallRateVal2.setVisibility(View.INVISIBLE);

                        holder.txtCallRate.setVisibility(View.INVISIBLE);

                        ////////Unclicking Highlight Delete and call Forwarding
                        holder.Delete.setBackgroundResource(R.drawable.number_delete_gray);
                        holder.Delete.setClickable(false);

                        holder.callForwardingSwitch.setChecked(false);
                        holder.callForwardingSwitch.setClickable(false);
                        // holder.callForwardingSwitch.setHighlightColor(ctx.getResources().getColor(R.color.text_dark_grey));
                        /////////Visible Tag
                        holder.rel__number_status.setVisibility(View.VISIBLE);
                        holder.rel__number_status.setBackground(ctx.getDrawable(R.drawable._submitted_tag));
                        holder.txtNumberStatus.setText("Registration Submitted *");

                        /////// Hide Renew Date:
                        holder.txtRenew.setVisibility(View.GONE);
                        holder.txtRenewDate.setVisibility(View.GONE);

                    }else  if(application_status.equals("1")){
                        holder.txtStatus.setText("Click here to complete number registration");
                        holder.txtStatus.setVisibility(View.VISIBLE);
                        /////////Visible Tag
                        holder.rel__number_status.setVisibility(View.VISIBLE);
                        holder.rel__number_status.setBackground(ctx.getDrawable(R.drawable._saved_tag));
                        holder.txtNumberStatus.setText("Registration Incomplete");

                        /////// Hide Renew Date:
                        holder.txtRenew.setVisibility(View.GONE);
                        holder.txtRenewDate.setVisibility(View.GONE);

                        holder.txtStatus.setTextColor(ctx.getResources().getColor(R.color.app_header));
                    }else  if(application_status.equals("2")){//////Approved///
                        //holder.txtStatus.setText("Approved");

                        holder.txtStatus.setVisibility(View.VISIBLE);
                        holder.txtStatus.setText("Active");
                        holder.txtStatus.setTextColor(ctx.getResources().getColor(R.color.green600));

                        /////////Hide Tag
                        holder.rel__number_status.setVisibility(View.INVISIBLE);

                        holder.txtSetupVal.setVisibility(View.GONE);
                        holder.txtSetup.setVisibility(View.GONE);
                        /////// VISIBLE Renew Date:
                        holder.txtRenew.setVisibility(View.VISIBLE);
                        holder.txtRenewDate.setVisibility(View.VISIBLE);
                    }else  if(application_status.equals("3")){///// Rejected///
                        //holder.txtStatus.setText("Rejected");

                        /////////Hide Tag
                        holder.txtStatus.setVisibility(View.INVISIBLE);
                        holder.rel__number_status.setVisibility(View.INVISIBLE);

                        /////// VISIBLE Renew Date:
                        holder.txtRenew.setVisibility(View.VISIBLE);
                        holder.txtRenewDate.setVisibility(View.VISIBLE);

                    }else{
                        if(cat.getLocal().equals("1") && cat.getAddress().equals("1")
                                && cat.getPhoto().equals("1") && cat.getapplication_status().equals("-1")){
                            holder.txtStatus.setVisibility(View.VISIBLE);
                            holder.txtStatus.setText("Active");
                            holder.txtStatus.setTextColor(ctx.getResources().getColor(R.color.green600));
                            holder.rel__number_status.setVisibility(View.INVISIBLE);

                            holder.txtSetupVal.setVisibility(View.GONE);
                            holder.txtSetup.setVisibility(View.GONE);
                        }else{
                            holder.txtStatus.setText("");
                            /////////Hide Tag
                            holder.txtStatus.setVisibility(View.INVISIBLE);
                            holder.rel__number_status.setVisibility(View.INVISIBLE);
                        }
                    }

                    System.out.println("Flag::::::::::: "+ flagPath);
                        /*Picasso.with(ctx)
                                .load(flagPath) // thumbnail url goes here
                                .into(holder.CountryFlagim, new Callback() {
                                    @Override
                                    public void onSuccess() {
                                        Picasso.with(ctx)
                                                .load(flagPath) // image url goes here
                                                .placeholder(holder.CountryFlagim.getDrawable())
                                                .into(holder.CountryFlagim);
                                    }

                                    @Override
                                    public void onError() {

                                    }
                                });*/
                    Glide
                            .with(ctx)
                            .load(flagPath)
                            .centerCrop()
                            .placeholder(holder.CountryFlagim.getDrawable())
                            .into(holder.CountryFlagim);
                    //Picasso.with(ctx).load(flagPath).into(holder.CountryFlagim);


                    applyClickEvents(holder,position);


                }


                @Override
                public int getItemCount() {
                    if (this.Modelslist != null) {
                        return this.Modelslist.size();
                    } else {
                        return 0;
                    }

                }

                private void applyClickEvents(final RecyclerAdapter.MyViewHolder holder, final int position) {

                    holder.mainFrame.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            listener.moveToDetailScreen(position);
                        }
                    });

                    holder.Delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final Model cat = Modelslist.get(position);

                        }
                    });


                    holder.callForwardingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            System.out.println("Call forwarding switch: "+ b);

                        }
                    });

                }


                public interface NumberListAdapterListener {

                    void deleteItem(int position);
                    void moveToDetailScreen(int position);

                }

                public class MyViewHolder extends RecyclerView.ViewHolder {
                    RelativeLayout rel__number,rel__setup,rel__number_status;
                    ImageView CountryFlagim;
                    TextView txtNumber, txtName;
                    SwitchCompat callForwardingSwitch;
                    TextView txtNumberDesc;
                    TextView txtSetupVal, txtSetup;
                    TextView txtMonthlyVal;
                    TextView txtCallRateVal, txtCallRateVal2, txtCallRate;
                    TextView txtStatus;
                    TextView txtNumberStatus, txtRenewDate, txtRenew;
                    Button Delete;
                    RelativeLayout mainFrame;


                    public MyViewHolder(View v) {
                        super(v);

                        rel__number = (RelativeLayout) v.findViewById(R.id.rel__number);
                        rel__setup = (RelativeLayout) v.findViewById(R.id.rel__setup);
                        rel__number_status = (RelativeLayout) v.findViewById(R.id.rel__number_status);
                        CountryFlagim = (ImageView) v.findViewById(R.id.CountryFlagim);
                        txtNumber = (TextView) v.findViewById(R.id.txtNumber);
                        txtName = (TextView) v.findViewById(R.id.txtName);

                        txtNumberDesc = (TextView) v.findViewById(R.id.txtNumberDesc);
                        txtSetupVal = (TextView) v.findViewById(R.id.txtSetupVal);
                        txtSetup = (TextView) v.findViewById(R.id.txtSetup);

                        txtMonthlyVal = (TextView) v.findViewById(R.id.txtMonthlyVal);
                        txtCallRate = (TextView) v.findViewById(R.id.txtCallRate);
                        txtCallRateVal = (TextView) v.findViewById(R.id.txtCallRateVal);
                        txtCallRateVal2 = (TextView) v.findViewById(R.id.txtCallRateVal2) ;
                        txtStatus = (TextView) v.findViewById(R.id.txtStatus);
                        txtNumberStatus = (TextView) v.findViewById(R.id.txtNumberStatus);
                        Delete = (Button) v.findViewById(R.id.Delete);
                        callForwardingSwitch = (SwitchCompat) v.findViewById(R.id.callForwardingSwitch);
                        mainFrame = (RelativeLayout) v.findViewById(R.id.mainFrame);
                        txtRenewDate = (TextView) v.findViewById(R.id.txtRenewDate);
                        txtRenew = (TextView) v.findViewById(R.id.txtRenew);

                    }
                }






            }

1 个答案:

答案 0 :(得分:0)

您可以尝试Glide。 Glide无法加载完整尺寸的图片,并且使用的内存更少,应该比毕加索更快。