简体   繁体   中英

Load More Items On Scroll Android

I have a listview which is fetching data from Firebase.

I want to at the end of scroll "load more items" appears in the footer of the list while loading more items and adding them to the adapter (for example 10 items each time). I have problem in implementing this feature. Please help me with it. Thanks.

My adapter

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) {
                          }
                      }
                );
    }
}
}

see this link ,have some good explanation.

add this code (EndlessRecyclerViewScrollListener.class) to your project. and use it like below.

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()`
 }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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