按钮的文本未显示在recyclerview中

时间:2018-09-24 12:28:12

标签: android button android-recyclerview

我在xml代码中有一个带有预定义文本的按钮,问题是当recyclerview列表中有很多项目时,在某些项目中按钮的文本没有显示。

在一个5到6个项目的列表中,它运作良好,当列表中有20到30个项目(在15个项目之后)时,会出现问题,而其中的某些项目没有显示文字。

2 个答案:

答案 0 :(得分:0)

这是RecyclerView代码

  mProfessionals.add(new Professional(id, city, name + last_name, specialty, image, rating));

  mProfessionalAdapter = new ProfessionalAdapter(getContext(), mProfessionals);
  mLayoutManager = configureLayoutManager();
  mRvOnlyProfessionals.setAdapter(mProfessionalAdapter);
  mRvOnlyProfessionals.setLayoutManager(mLayoutManager);

这是Adapter

public class ProfessionalAdapter extends RecyclerView.Adapter<ProfessionalAdapter.ViewHolder> {

    private Context context;
    private List<Professional> professionals;

    public ProfessionalAdapter(Context context, List<Professional> professionals) {
        this.context = context;
        this.professionals = professionals;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.details_list_directory, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final String id = String.valueOf(professionals.get(position).getId());
        String name = professionals.get(position).getName();
        String specialty = professionals.get(position).getSpecialty();
        String city = professionals.get(position).getCity();
        String rating = professionals.get(position).getRating();
        String image = professionals.get(position).getImage();
        holder.bind(context, name, specialty, city, rating, image);
        holder.mBtnResultViewProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("ProfeId", id);
                intent.putExtra("ValorF", 1);
                intent.putExtra("Uid", "1");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                v.getContext().startActivity(intent);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTvResultName, mTvResultSpecialty, mTvResultCity;
        public ImageView mIvResultImage;
        public RatingBar mRbResultRatings;
        public Button mBtnResultViewProfile;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvResultName = (TextView) itemView.findViewById(R.id.tvResultName);
            mTvResultSpecialty = (TextView) itemView.findViewById(R.id.tvResultSpecialty);
            mTvResultCity = (TextView) itemView.findViewById(R.id.tvResultCity);
            mIvResultImage = (ImageView) itemView.findViewById(R.id.ivResultImage);
            mRbResultRatings = (RatingBar) itemView.findViewById(R.id.rbResultRatings);
            mBtnResultViewProfile = (Button) itemView.findViewById(R.id.btnResultViewProfile);
        }

        public void bind(Context context, final String name, final String specialty, final String city, final String rating, final String image) {
            this.mTvResultName.setText(name);
            this.mTvResultSpecialty.setText(specialty);
            this.mTvResultCity.setText(city);

            if (!rating.isEmpty()) {
                if (rating.equals("0.00")) {
                    this.mRbResultRatings.setRating(0);
                } else if (rating.contains(".")) {
                    this.mRbResultRatings.setRating(Integer.parseInt(rating.split(".")[0]));
                } else {
                    this.mRbResultRatings.setRating(Integer.parseInt(rating));
                }
            }

            if (!image.isEmpty()) {
                Picasso.with(context).load(image).transform(new CircleTransform()).into(this.mIvResultImage);
            } else {
                Picasso.with(context).load(R.drawable.image_placeholder).transform(new CircleTransform()).into(this.mIvResultImage);
            }
        }
    }
}

这是XML Buttom代码

<Button
   android:id="@+id/btnResultViewProfile"
   android:layout_width="150dp"
   android:layout_height="30dp"
   android:background="@color/header"
   android:text="Ver Perfil"
   android:textColor="@color/white"/>

答案 1 :(得分:0)

This is the problem

如果我再次向上滚动,它不会显示以前的内容