0

私は GridLayoutManager の web サービス データと endlessrecyclerview を統合したいので、以下は私のコードです

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 のフォーカスが何度も何度も下にスクロールしませんか? どうすればこの問題を解決できますか?あなたのすべての提案はかなりのものです

4

3 に答える 3

0

setOnScrollListener() の代わりに addOnScrollListener() を使用する

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


        }
    });
于 2016-05-30T09:40:01.923 に答える
0

クラス getData->onPostExecute から次の 2 行を削除します。

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

これらの行 (2 行以上) を GridRecycle.java-> onCreate のこの行の下に追加します。

mRecyclerView.setLayoutManager(mLayoutManager);
于 2016-05-30T12:26:41.343 に答える
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);
于 2016-05-31T09:27:48.110 に答える