我想将无限循环器视图与 GridLayoutManager 中的网络服务数据集成,所以下面是我的代码
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);
}
当我运行上面的代码并向下滚动时,我的recycleview焦点一次又一次地不连续向下滚动?所以知道我该如何解决这个问题吗?你的所有建议都是可观的