向下滚动

时间:2015-06-30 07:33:15

标签: android android-layout android-cardview

这是我的代码。我有卡片视图,有些我有一张照片,有些没有照片。但是在向上和向下滚动之后我会在每个CardView(来自之前的cardview的图片)事件中获得图片,如果根本不应该有任何图片(***。size()= 0)。我认为问题出在这里

    if (mDataset.get(position).getAdv_pics().size()>0){
            String pic = mDataset.get(position).getAdv_pics().get(0);
            ImageView image = new ImageView(mActivity);
            Log.d("!!!!", position + " http://m2.biz.ua" + holder.pic + "_small.jpg");
            holder.mLayout.addView(image, 1);
            Picasso.with(mActivity)
                    .load("http://m2.biz.ua" + pic + "_small.jpg")
                    .resize(100, 100)
                    .centerCrop()
                    .into(image);
    }

FlatListCardAdapter.class

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


    private List<Flat> mDataset;
    private Activity mActivity;
    private String[] currency;

    private OnCardClickListener mListener;


    public interface OnCardClickListener{
        void getFlat(Flat flatId);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mAdvText, mAdvRooms, mAdvPrice, mAdvAddr;
        public LinearLayout mLayout;
        public ImageButton mCallButton;
        public Button test;
        public String pic;
        public ImageView smallImage;
        public View view;

        private CardView mCardView;

        public ViewHolder(View v ) {
            super(v);
            view=v;
            mAdvText = (TextView) v.findViewById(R.id.advText);
            mAdvRooms = (TextView) v.findViewById(R.id.advRooms);
            mAdvPrice = (TextView) v.findViewById(R.id.advPrice);
            mAdvAddr = (TextView) v.findViewById(R.id.advAddr);
            mLayout = (LinearLayout) v.findViewById(R.id.picMainLayout);
            mCallButton = (ImageButton) v.findViewById(R.id.callAdv);
            mCardView = (CardView) v.findViewById(R.id.card_view);
        }
    }

    public FlatListCardAdapter(Activity activity, List<Flat> dataset, OnCardClickListener mListener) {
        mDataset = dataset;
        mActivity = activity;
        this.mListener = mListener;
    }

    @Override
    public FlatListCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_realty_list_card_custom, parent, false);

        ViewHolder vh = new ViewHolder(v);
        currency=mActivity.getResources().getStringArray(R.array.currency);

        return vh;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final Flat mFlat = mDataset.get(position);
        String mCurrency="";

        if ( Integer.parseInt(mFlat.getAdv_currency())>0 ){
            mCurrency=currency[Integer.parseInt(mFlat.getAdv_currency())];
        }

        holder.mAdvRooms.setText(mFlat.getAdv_rooms()+" ");
        holder.mAdvPrice.setText(mFlat.getAdv_price()+" "+mCurrency);
        holder.mAdvText.setText(mFlat.getAdv_text());
        holder.mAdvAddr.setText(mFlat.getAdv_addr());


        holder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("!!!!", mFlat.getId());

                try {
                    mListener = (MainActivity) mActivity;
                    mListener.getFlat(mFlat);
                } catch (ClassCastException e) {
                    throw new ClassCastException(mActivity.toString() + " must implement OnArticleSelectedListener");
                }

            }
        });


        holder.mCallButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> phone = mFlat.getAdv_phone();
                if (phone.size()>0) {
                    Toast.makeText(mActivity, phone.get(0), Toast.LENGTH_SHORT).show();
                }
            }
        });


        Log.d("!!!!", "SIZE: " +mDataset.get(position).getAdv_pics().size());

        if (mDataset.get(position).getAdv_pics().size()>0){
                String pic = mDataset.get(position).getAdv_pics().get(0);
                ImageView image = new ImageView(mActivity);
                Log.d("!!!!", position + " http://****" + holder.pic + "_small.jpg");
                holder.mLayout.addView(image, 1);
                Picasso.with(mActivity)
                        .load("http://****" + pic + "_small.jpg")
                        .resize(100, 100)
                        .centerCrop()
                        .into(image);
        }
        /**/
    }

    @Override
    public int getItemCount() {
        if (mDataset==null){
            return 0;
        }
        return mDataset.size();
    }

}

1 个答案:

答案 0 :(得分:2)

项目视图在RecyclerView中重复使用。问题在这里

  if (mDataset.get(position).getAdv_pics().size()>0){
        ...
  }

添加else子句并将图像设置为null。如下面的代码所示

  if (mDataset.get(position).getAdv_pics().size()>0){
        ...
  }else{
    image.setImageBitmap(null);
  }