如何为gridview实现Endless滚动侦听器

时间:2015-07-27 09:38:10

标签: android gridview baseadapter infinite-scroll

我一直在尝试为ecommmerce应用的产品详情页面实现无限滚动功能。网格显示项目的详细信息,当我滚动到底部时,我需要显示进度条,然后附加新的项目网格。

api调用的工作原理如下,我需要发送一个start_row_number和limit,它会将start_row_number中的所有项目发送给我。示例:start_row_number = 0和limit = 10.这将返回0到10之间的项目

之后我需要在用户到达网格底部时加载更多项目,并将其附加到gridview。所以我将发送start_row_number = 10和limit = 10,这将返回10到20的项目。

截至目前,我可以获得0到10之间的项目,但之后不会。我如何创建无限滚动功能,并使一切不会给我带来错误,例如在主线程上做太多工作'

这是我的MainActivity:

public class ProductListing extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_listing_act);
    init();
}

public void productListingApiCall(ProductListingCondtionModel productListingCondtionModel) {

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(productListingCondtionModel.getBase_url()).setLogLevel(RestAdapter.LogLevel.FULL).build();
    final ProductListingApi productListingApi =
            restAdapter.create(ProductListingApi.class);
    productListingApi.getFeed(productListingCondtionModel.getFile(),
            productListingCondtionModel.getOperation_condition(),
            productListingCondtionModel.getSearch_string_condition(),
            productListingCondtionModel.getMinprice_condition(),
            productListingCondtionModel.getMaxprice_condition(),
            productListingCondtionModel.getMincusratings_condition(),
            productListingCondtionModel.getMaxcusratings_condition(),
            productListingCondtionModel.getDiscount_condition(),
            productListingCondtionModel.getCatids_condition(),
            productListingCondtionModel.getBrands_condition(),
            productListingCondtionModel.getAffids_condition(),
            productListingCondtionModel.getStart_row_condition(),
            productListingCondtionModel.getLimit(),
            productListingCondtionModel.getOrderby_condition(),
            productListingCondtionModel.getSortby_condition(), new Callback<ProductListingPojo>() {

                @Override
                public void success(ProductListingPojo productListingPojo, Response response) {
                    final ProductListingPojo product = productListingPojo;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String[] t = Arrays.copyOf(product.getTitle(),
                                    product.getTitle().length);
                            int[] p = Arrays.copyOf(product.getSellingprice(),
                                    product.getSellingprice().length);
                            int[] m = Arrays.copyOf(product.getMrp(),
                                    product.getMrp().length);
                            int[] d = Arrays.copyOf(product.getDiscountpercent(),
                                    product.getDiscountpercent().length);
                            String[] i = Arrays.copyOf(product.getProductimageSmall1(),
                                    product.getProductimageSmall1().length);


                            for(int j = 0; j < t.length; j++) {
                                CategoryAllApi categoryAllApi = new CategoryAllApi();
                                categoryAllApi.setTitle(t[j]);
                                categoryAllApi.setPrice(p[j]);
                                categoryAllApi.setMrp(m[j]);
                                categoryAllApi.setDiscount(d[j]);
                                categoryAllApi.setImage(i[j]);
                                arrayList.add(categoryAllApi);
                            }



                        }
                    }).run();
                    setAdapter();
                }

                @Override
                public void failure(RetrofitError error) {
                    tv_title_header.setText(error.getMessage());
                    Log.e("error", error.getMessage());
                }
            });
}

void setAdapter() {
    adapter = new ProductListingGridAdapter(this, arrayList);
    gv_product_listing_act.setAdapter(adapter);
}
}

继承人:

public class ProductListingGridAdapter extends BaseAdapter {

public ProductListingGridAdapter(ProductListing productListing, ArrayList<CategoryAllApi> arrayList) {
    this.arrayList= arrayList;
    context = productListing;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

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

public class Holder
{
    ImageView im_pic;
    TextView tv_title, tv_price, tv_mrp, tv_discount;
}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    GridView grid = (GridView) parent;
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    grid.setColumnWidth(width);
    grid.setNumColumns(2);
    int size = grid.getRequestedColumnWidth() / 2 ;
    Double d = new Double(size * 2);
    int h = d.intValue();

    Holder holder = new Holder();
    View rowView;


    int index = grid.getFirstVisiblePosition();
    View v = grid.getChildAt(0);
    int top = (v == null) ? 0 : (v.getTop() - grid.getPaddingTop());

    grid.setSelectionFromTop(index, top);

    rowView = inflater.inflate(R.layout.product_listing_gv_items_lay, null);
    rowView.setLayoutParams(new GridView.LayoutParams(size, h));

    holder.im_pic = (ImageView) rowView.findViewById(R.id.im_product_listing_gv_items_lay_pic);
    holder.tv_title = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_title);
    holder.tv_price = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_price);
    holder.tv_mrp = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_mrp);
    holder.tv_discount = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_discount);

    holder.tv_title.setTypeface(EasyFonts.robotoMedium(rowView.getContext()));
    holder.tv_price.setTypeface(EasyFonts.robotoBlack(rowView.getContext()));
    holder.tv_mrp.setTypeface(EasyFonts.robotoLight(rowView.getContext()));
    holder.tv_mrp.setPaintFlags(holder.tv_mrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    holder.tv_discount.setTypeface(EasyFonts.robotoLight(rowView.getContext()));

    categoryAllApi = arrayList.get(position);
    Ion.with(holder.im_pic).load(categoryAllApi.getImage());
    holder.tv_title.setText(categoryAllApi.getTitle());
    holder.tv_price.setText("Rs. " + categoryAllApi.getPrice());
    holder.tv_mrp.setText("Rs. " + categoryAllApi.getMrp());
    holder.tv_discount.setText("" + categoryAllApi.getDiscount() + "%");

    rowView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ProductDetails.class);
            Bundle bundle = new Bundle();
            bundle.putString("operation", "");
            bundle.putString("productkey", "");
            intent.putExtras(bundle);
            context.startActivity(intent);

        }
    });

    return rowView;
}

}

继承了CategoryApiCall.java:

public class CategoryAllApi {

private String title, image;
private int price, mrp, discount;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public int getMrp() {
    return mrp;
}

public void setMrp(int mrp) {
    this.mrp = mrp;
}

public int getDiscount() {
    return discount;
}

public void setDiscount(int discount) {
    this.discount = discount;
}
}

1 个答案:

答案 0 :(得分:0)

我看到你使用过GridView。

Google已经推出了RecyclerView,克服了listview和Gridview的缺陷,这些漏洞充斥着ram并使app变得笨重。

将RecyclerView与GridLayoutManager一起使用。此外,在RecyclerView中研究无限滚动(这很容易实现)。

我强烈建议您使用此应用,因为我尝试了同样的结果,结果非常出色。应用程序更快,内存变轻,滚动效果很好。此外,还有许多功能,如recyclerView对象将提供。关于谷歌处理这些事情并提高应用程序质量的时间。

流程可能看起来像一项任务,但从长远来看,它会为您和应用用户提供很多帮助。