繁体   English   中英

在Scroll Android上加载更多项目

[英]Load More Items On Scroll Android

我有一个从Firebase获取数据的列表视图。

我想在滚动末尾“加载更多项目”出现在列表的页脚中,同时加载更多项目并将其添加到适配器(例如,每次10个项目)。 我在实现此功能时遇到问题。 请帮我。 谢谢。

我的适配器

public class MarkerAdapter extends RecyclerView.Adapter<MarkerAdapter.ViewHolder>{
private final LayoutInflater mLayoutInflater;
private final List<Marker> mMarkerList;

public interface OnItemClickListener {
    void onItemClick(Marker marker);
}

private OnItemClickListener mOnItemClickListener;

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    mOnItemClickListener = onItemClickListener;
}

public MarkerAdapter(Context MARVIN) {
    mLayoutInflater = LayoutInflater.from(MARVIN);
    mMarkerList = new ArrayList<>();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemMarker = mLayoutInflater.inflate(R.layout.item_marker, parent, false);
    return new ViewHolder(itemMarker);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Marker marker = mMarkerList.get(position);
    holder.bindMarker(marker);
    holder.loadImageFromStorage(position);
}

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

public void refreshMarkers(List<Marker> markers) {
    mMarkerList.clear();
    mMarkerList.addAll(markers);
    notifyDataSetChanged();
}


public List<Marker> getMarkerList() {
    return mMarkerList;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private final ImageView mImageView;
    private final TextView mNameTextView;
    private final TextView mAuthorTextView;

    private Marker mMarker;

    public ViewHolder(View itemView) {
        super(itemView);
        mImageView = (ImageView) itemView.findViewById(R.id.photo_image_view);
        mNameTextView = (TextView) itemView.findViewById(R.id.text_view_name);
        mAuthorTextView = (TextView) itemView.findViewById(R.id.text_view_author);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(mMarker);
                }
            }
        });
    }

    void bindMarker(Marker marker) {
        mMarker = marker;
        mNameTextView.setText(mMarker.getName());
        mAuthorTextView.setText(mMarker.getAuthor());
        Picasso.with(mImageView.getContext())
                .load(mMarker.getImage().getUrl())
                .placeholder(R.color.grey_400)
                .resize(1100, 1100)
                .into(mImageView);
    }
    private void loadImageFromStorage(int i) {
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path + "/save/");
        File f = new File(dir, String.valueOf(i)+"photo.jpg");
        Log.d("loaded from storage ", f.getPath());

    }

    void cacheMarkers(final int i) {
        Picasso.with(mImageView.getContext())
                .load(mMarker.getImage().getUrl())
                .into(new Target() {
                          @Override
                          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                              File path = Environment.getExternalStorageDirectory();
                              File dir = new File(path + "/save/");
                              dir.mkdirs();
                              File file = new File(dir, String.valueOf(i) +"photo.jpg");
                              Log.d("cache image", file.getPath());
                              OutputStream out = null;
                              try {
                                  out = new FileOutputStream(file);
                                  bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                                  out.flush();
                                  out.close();
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }

                          }

                          @Override
                          public void onBitmapFailed(Drawable errorDrawable) {
                          }

                          @Override
                          public void onPrepareLoad(Drawable placeHolderDrawable) {
                          }
                      }
                );
    }
}
}

看到此链接 ,有一些很好的解释。

将此代码 (EndlessRecyclerViewScrollListener.class)添加到您的项目中。 并按如下所示使用它。

public class MainActivity extends Activity {
// Store a member variable for the listener
private EndlessRecyclerViewScrollListener scrollListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // Configure the RecyclerView
   RecyclerView rvItems = (RecyclerView) findViewById(R.id.rvContacts);
   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
   rvItems.setLayoutManager(linearLayoutManager);
   // Retain an instance so that you can call `resetState()` for fresh searches
   scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
       @Override
       public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
           // Triggered only when new data needs to be appended to the list
           // Add whatever code is needed to append new items to the bottom of the list
           loadNextDataFromApi(page);
       }
  };
  // Adds the scroll listener to RecyclerView
  rvItems.addOnScrollListener(scrollListener);
 }

 // Append the next page of data into the adapter
 // This method probably sends out a network request and appends new data 
/// items to your adapter. 
 public void loadNextDataFromApi(int offset) {
      // Send an API request to retrieve appropriate paginated data 
      //  --> Send the request including an offset value (i.e `page`) as a 
    query parameter.
        //  --> Deserialize and construct new model objects from the API //response
     //  --> Append the new data objects to the existing set of items inside the array of items
  //  --> Notify the adapter of the new items made with `notifyItemRangeInserted()`
 }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM