GridLayoutManager与EndlessRecyclerOnScrollListener无法正常工作的android

时间:2016-05-30 09:25:42

标签: android android-recyclerview gridlayoutmanager

我想将endlessrecyclerview与GridLayoutManager中的web服务数据集成,所以下面是我的代码

public class GridRecycle extends AppCompatActivity {

RecyclerView mRecyclerView;
private GridLayoutManager mLayoutManager;
ArrayList<GetProductListing.ListingBlock>rowListItem;
GetProductListing mGetProductListing;
PostParseGet mPostParseGet;
int pager=1;
RecyclerViewAdapter rcAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_gird);
    rowListItem=new ArrayList<>();
    mPostParseGet=new PostParseGet(GridRecycle.this);
    mGetProductListing=new GetProductListing();
    mRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);
    mLayoutManager = new GridLayoutManager(GridRecycle.this, 2 );

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(mLayoutManager);
    new getData().execute();



    mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(
            mLayoutManager) {
        @Override
        public void onLoadMore(int current_page) {
            pager++;
            new getData().execute();
        }

    });
}

public class getData extends AsyncTask<Void,Void,Void>
{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        mGetProductListing=(GetProductListing)mPostParseGet.getProductDataWithSlug(mGetProductListing,pager);

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (mGetProductListing.getListing().getData().size()>0)
        {
            rowListItem=getProductBlock();
            rcAdapter= new RecyclerViewAdapter(GridRecycle.this, Tags.mAllListingBlocks);
            mRecyclerView.setAdapter(rcAdapter);
            rcAdapter.notifyDataSetChanged();
        }
    }
}
public ArrayList<GetProductListing.ListingBlock> getProductBlock() {
    ArrayList<GetProductListing.ListingBlock> mArrayListParserLog = new ArrayList<GetProductListing.ListingBlock>();


    for (int i = 0; i < mGetProductListing.getListing().getData().size(); i++) {
        GetProductListing.ListingBlock mParserLog = new GetProductListing.ListingBlock();

        mParserLog.setId(mGetProductListing.getListing().getData().get(i).getId());
        mParserLog.setTitle(mGetProductListing.getListing().getData().get(i).getTitle());
        mParserLog.setDiscountSale(mGetProductListing.getListing().getData().get(i).getDiscountSale());
        mParserLog.setDiscountSelling(mGetProductListing.getListing().getData().get(i).getDiscountSelling());
        mParserLog.setPrice(mGetProductListing.getListing().getData().get(i).getPrice());
        mParserLog.setSale_end(mGetProductListing.getListing().getData().get(i).getSale_end());
        if (mGetProductListing.getListing().getData().get(i).getSale_price()!=null && !mGetProductListing.getListing().getData().get(i).getSale_price().isEmpty())
        {
            mParserLog.setSale_price(mGetProductListing.getListing().getData().get(i).getSale_price());

        }
        else
        {
            mParserLog.setSale_price("0");

        }
        mParserLog.setSale_start(mGetProductListing.getListing().getData().get(i).getSale_start());
        mParserLog.setSelling_price(mGetProductListing.getListing().getData().get(i).getSelling_price());
        mParserLog.setProduct_rating(mGetProductListing.getListing().getData().get(i).getProduct_rating());
        mParserLog.setSlug(mGetProductListing.getListing().getData().get(i).getSlug());
        mParserLog.setSold_out(mGetProductListing.getListing().getData().get(i).getSold_out());
        mParserLog.setImage(mGetProductListing.getListing().getData().get(i).getImage());
        mParserLog.setSku_id(mGetProductListing.getListing().getData().get(i).getSku_id());
        mParserLog.setMedium_image(mGetProductListing.getListing().getData().get(i).getMedium_image());
        mArrayListParserLog.add(mParserLog);
        Tags.mAllListingBlocks.add(mParserLog);

    }
    return mArrayListParserLog;
}

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<GetProductListing.ListingBlock> itemList;
    private Context context;

    public RecyclerViewAdapter(Context context, List<GetProductListing.ListingBlock> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_reow, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
        holder.name.setText(itemList.get(position).getTitle());
        holder.price.setText(itemList.get(position).getPrice());


    }

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

EndlessRecyclerOnScrollListener.java

public abstract class EndlessRecyclerOnScrollListener extends
    RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
        .getSimpleName();

private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 1;

private LinearLayoutManager mLinearLayoutManager;

public EndlessRecyclerOnScrollListener(
        LinearLayoutManager linearLayoutManager) {
    this.mLinearLayoutManager = linearLayoutManager;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
        if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
        }
    }
    if (!loading
            && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
        // End has been reached

        // Do something
        current_page++;

        onLoadMore(current_page);

        loading = true;
    }
  }

 public abstract void onLoadMore(int current_page);
}

当我运行上面的代码并向下滚动时,我的再循环视图一次又一次地聚焦在顶部而不是连续向下滚动?所以任何想法我怎么能解决这个问题?你的所有建议都很明显

3 个答案:

答案 0 :(得分:0)

使用addOnScrollListener()而不是setOnScrollListener()

listRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);


        }
    });

答案 1 :(得分:0)

从类getData-&gt; onPostExecute中删除以下两行。

rcAdapter= new RecyclerViewAdapter(GridRecycle.this, Tags.mAllListingBlocks);
            mRecyclerView.setAdapter(rcAdapter);

并在GridRecycle.java-&gt;中添加这些行(超过2行) onCreate,低于这一行

mRecyclerView.setLayoutManager(mLayoutManager);

答案 2 :(得分:0)

我解决了我的问题

public abstract class EndlessRecyclerGridOnScrollListener extends
RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerGridOnScrollListener.class
    .getSimpleName();

  private int previousTotal = 0;
  private boolean loading = true;
  private int visibleThreshold = 5;
   int firstVisibleItem, visibleItemCount, totalItemCount;

   private int current_page = 1;

  private GridLayoutManager mLinearLayoutManager;

   public EndlessRecyclerGridOnScrollListener(
    GridLayoutManager linearLayoutManager) {
  this.mLinearLayoutManager = linearLayoutManager;
 }

  @Override
 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
 super.onScrolled(recyclerView, dx, dy);

visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

if (loading) {
    if (totalItemCount > previousTotal) {
        loading = false;
        previousTotal = totalItemCount;
    }
}
if (!loading
        && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
    // End has been reached

    // Do something
    current_page++;

    onLoadMore(current_page);

    loading = true;
   }
 }

  public abstract void onLoadMore(int current_page);
 }
GridRecycle.java文件中的

mGridView.setOnScrollListener(new EndlessRecyclerGridOnScrollListener(
        mLayoutManagerGrid) {
    @Override
    public void onLoadMore(int current_page) {
        if (Tags.mArrayListingAllProductBlocks.size() < totalitem) {
            page++;
            new productList().execute();
        }
    }

});

并删除onPost方法中的2行

rcAdapter= new RecyclerViewAdapter(GridRecycle.this, Tags.mAllListingBlocks);
mRecyclerView.setAdapter(rcAdapter);