android RecyclerView

时间:2016-02-09 10:47:43

标签: android android-recyclerview infinite-scroll

我有Android应用程序,我在Recylerview中显示产品数据列表来自Web服务。但是我遇到了一个奇怪的问题,当我向下滚动Recyclerview时,一些项目内容没有出现。当向下滚动更多项目并向上滚动到该项目时,会显示此内容。我不明白这个问题。内容未加载时有图像。

enter image description here

当内容以向上滚动方式显示时,会有另一张图像 enter image description here

我使用此library。有适配器项xml ...

<?xml version="1.0" encoding="utf-8"?>

card_view:cardCornerRadius="5dp"
>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/containerColor"
    android:padding="5dp"
    android:orientation="vertical"
    >
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:button="@drawable/fav_checkbox_selector"
        android:background="@null"
        android:id="@+id/fav"
        />

    <ImageView
        android:layout_marginTop="15dp"
        android:layout_below="@+id/fav"
        android:layout_centerHorizontal="true"
        android:layout_width="@dimen/produtcsimageWidth"
        android:layout_height="@dimen/productsimageheight"
        android:background="@null"
        android:id="@+id/img_productImage"
        />
    <TextView
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="5dp"
        android:layout_below="@+id/img_productImage"
        android:id="@+id/tv_productName"
        style="@style/productsNameStyle"
        android:text="Name"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        />
    <LinearLayout
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:layout_marginBottom="5dp"
            android:layout_below="@+id/tv_productName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
        >
    <TextView
        android:id="@+id/tv_productPrice"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/differentButtonColor"
        android:textSize="12sp"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Price"
        />

    <TextView
            android:id="@+id/tv_productOldPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/tv_productPrice"
            android:textColor="@color/textSecondarColor"
            android:textSize="10sp"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="p2"
            android:layout_marginTop="1dp"
            />
    </LinearLayout>
</RelativeLayout>

我有一个代码片段,我在其中加载数据..

 public void onEvent(ProductsResponse productsResponse) {




    if (productList != null ) {
        int range = productAdapter.getItemCount()+1;
       // productAdapter.products.addAll(productsResponse.getProducts());
        productAdapter.addAll(productsResponse.getProducts());

        System.out.print("Size" + productList.size());
        productAdapter.notifyItemRangeInserted(range,productsResponse.getProducts().size());

    } 
  else {
        productList = productsResponse.getProducts();
        productAdapter = new ProductAdapter(getActivity(), productList);
        listProduct.setAdapter(productAdapter);



    }      

这是我的适配器代码

    public class ProductAdapter   extends RecyclerView.Adapter implements Filterable {
    private static final int VIEW_PROG =4 ;
    public  List<ProductModel>products;
    boolean checkedTracks[];
    public  int ViewFormat= ViewType.GRID;
     protected Context context;
      protected OnItemClickListener mItemClickListener;
     protected List<ProductModel>filteredData;

  /*  public ProductAdapter( Context context,List<CategoryDetails> products)
    {
        this.products=products;
        this.context=context;
    }*/

public ProductAdapter( Context context,List productsList)
        {
        try {
        this.products = new ArrayList<>();
        checkedTracks=new boolean[productsList.size()];
        //this.products.addAll(productsList);
        this.products=productsList;
        this.context = context;
        Log.d("adapterSize", "" + productsList.size());
        filteredData=new ArrayList<>();
        filteredData.addAll(productsList);
        }
        catch (Exception ex)
        {
        ex.printStackTrace();
        }
        }
    public ProductAdapter( Context context,List productsList,int viewType)
    {
        this(context,productsList);
        ViewFormat=viewType;
    }
    public void addAll( List<ProductModel>products)
    {
        this.products.addAll(products);
    }
@Override
public  RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        int layout = 0;
        if(viewType==ViewType.GRID)
        layout=R.layout.item_products_grid;
        else if(viewType==ViewType.LIST)
        layout=R.layout.item_product_list;
         else if(viewType==ViewType.SINGLE)
            layout=R.layout.item_product_single;
        else if(viewType==ViewType.HOMEPAGEVIEW)
            layout=R.layout.item_homepage_product;
       /*else if(viewType==VIEW_PROG)
        {
            layout=R.layout.item_progress;

        }*/


        View itemView = LayoutInflater.
        from(parent.getContext()).
        inflate(layout,parent, false);

        return new ProductSummaryHolder(itemView);
        }

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

 //   return ViewFormat;
        //return super.getItemViewType(position);
        }

@Override
public void onBindViewHolder( RecyclerView.ViewHolder bindViewHolder, final int position) {
        try {
          if(bindViewHolder instanceof ProductSummaryHolder) {
              ProductModel productModel = products.get(position);
              ProductSummaryHolder holder = (ProductSummaryHolder) bindViewHolder;
              holder.productName.setText(productModel.getName());
              System.out.println(holder.productName.getText().toString() + "," + productModel.getName());
              holder.productPrice.setText(productModel.getProductPrice().getPrice());
              holder.productOldPrice.setText(productModel.getProductPrice().getOldPrice());
              Picasso.with(context).load(productModel.getDefaultPictureModel().getImageUrl()).
                      fit().centerInside().into(holder.productImage);
              holder.fav.setTag(new Integer(position));
          }
         /* else {
              ((ProgressViewHolder) bindViewHolder).progressBar.setIndeterminate(true);
          }*/


        }catch (ClassCastException ex)
        {

        }


        }



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

public interface OnItemClickListener {
    public void onItemClick(View view , int position);
}
    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }


public  class ProductSummaryHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected ImageView productImage;
    protected TextView productPrice;
    protected TextView productOldPrice;
    protected TextView productName;
    protected CheckBox fav;

    public ProductSummaryHolder(View itemView) {
        super(itemView);
        productImage = (ImageView) itemView.findViewById(R.id.img_productImage);
        productPrice = (TextView) itemView.findViewById(R.id.tv_productPrice);
        productName = (TextView) itemView.findViewById(R.id.tv_productName);
        productOldPrice = (TextView) itemView.findViewById(R.id.tv_productOldPrice);
        productOldPrice.setPaintFlags(productOldPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        fav=(CheckBox)itemView.findViewById(R.id.fav);
        fav.setVisibility(View.GONE);
        itemView.setOnClickListener(this);
        //fav.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(v, getPosition());
        }


    }

}

1 个答案:

答案 0 :(得分:1)

你的适配器数据只有一种视图类型吗? 如果数据的视图类型不同,则必须使用正确的逻辑覆盖getItemViewType(),但不能返回常量