在Scroll Android上加载更多项目

时间:2018-05-18 21:23:30

标签: android firebase view

我有一个listview,它从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) {
                          }
                      }
                );
    }
}
}

1 个答案:

答案 0 :(得分:1)

this link ,有一些很好的解释。

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