当RecyclerView达到结束时,它将加载另一对数据
但在加载RecyclerView时,RecyclerView的空视图是可见的,如果单击空视图,它会给我 ArrayBoundaryExeption
所以我需要在isLoading
时停止滚动 mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUserAdapter = new UserAdapter();
mRecyclerView.setAdapter(mUserAdapter);
mUserAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically(); */
// if (Choose.equals("Local")) {
Log.e("haint", "Load More");
mUsers.add(null);
mUserAdapter.notifyItemInserted(mUsers.size() - 1);
dataLoadMore();
//mRecyclerView.setNestedScrollingEnabled(false);
// mRecyclerView.stopScroll();
//Load more data for reyclerview
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically(); */
Log.e("haint", "Load More 2");
// mRecyclerView.setNestedScrollingEnabled(false);
// mRecyclerView.stopScroll();
//Remove loading item
if (!mUsers.isEmpty()) {
mUsers.remove(mUsers.size() - 1);
mUserAdapter.notifyItemRemoved(mUsers.size());
}
for (int i = 0; i < lenMore; i++) {
LocalListView localListView = new LocalListView();
localListView.setJob_local_id(job_local_idM[i]);
localListView.setJob_typess(job_typessM[i]);
mUsers.add(localListView);
}
mUserAdapter.notifyDataSetChanged();
mUserAdapter.setLoaded();
}
}, 5000);
}
});
RecycleClick.addTo(mRecyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// YOUR CODE
if(mUserAdapter.isLoading){
//
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new
CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically(); */
Toast.makeText(getApplicationContext(),"Loading please
wait...",Toast.LENGTH_LONG).show();
}else if(position!=-1){
Intent is = new Intent(getApplicationContext(), WorkProfileLocal.class);
is.putExtra("job_local_idG", mUsers.get(position).getJob_local_id());
is.putExtra("user_idsG", mUsers.get(position).getUser_ids());
startActivity(is);
}
}
});
static class UserViewHolder extends RecyclerView.ViewHolder {
public TextView tvJobTitle;
public TextView tvJobWork;
public UserViewHolder(View itemView) {
super(itemView);
tvHirerName = (TextView) itemView.findViewById(R.id.tvWhatWork);
tvJobTitle = (TextView) itemView.findViewById(R.id.tvJobTitle);
}
}
static class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
}
}
class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private OnLoadMoreListener mOnLoadMoreListener;
private boolean isLoading;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
public UserAdapter() {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically();*/
}
isLoading = true;
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically(); */
}
}
});
}
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener;
}
@Override
public int getItemViewType(int position) {
return mUsers.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(WorkNow.this).inflate(R.layout.list_view_local_jobs, parent, false);
return new UserViewHolder(view);
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(WorkNow.this).inflate(R.layout.layout_loading_item, parent, false);
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically(); */
return new LoadingViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof UserViewHolder) {
LocalListView localListView = mUsers.get(position);
UserViewHolder userViewHolder = (UserViewHolder) holder;
userViewHolder.tvHirerName.setText(localListView.getUser_names());
userViewHolder.tvJobTitle.setText(localListView.getJob_titles().replaceAll("_", " "));
if (localListView.getImage().isEmpty()) {
userViewHolder.hirerPicLocal.setImageResource(R.drawable.blank_low);
} else{
Picasso.with(getApplicationContext())
.load(localListView.getImage())
.into(userViewHolder.hirerPicLocal);
}
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
/* mRecyclerView.stopNestedScroll();
mRecyclerView.stopScroll();
CustomGridLayoutManager ss= new CustomGridLayoutManager(WorkNow.this);
ss.setScrollEnabled(false);
mRecyclerView.setNestedScrollingEnabled(false);
linearLayoutManager.canScrollVertically(); */
}
}
@Override
public int getItemCount() {
return mUsers == null ? 0 : mUsers.size();
}
public void setLoaded() {
isLoading = false;
}
}
这个Class我从StackOverFlow到StopScroll但它没有工作。
public class CustomGridLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true;
public CustomGridLayoutManager(Context context) {
super(context);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollVertically() {
//Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
return isScrollEnabled && super.canScrollVertically();
}
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WorkNow.this) {
@Override
public boolean canScrollVertically() {
return false;
}
};