请注意:我已经检查了SO上的其他问题,虽然我可以消除错误,但它还有其他问题。
下面是我现有的代码:
我的适配器类:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private IOnLoadMoreListener mIOnLoadMoreListener;
private int visibleThreshold = 3;
private int lastVisibleItem, totalItemCount;
private int[] lastVisibleItems = new int[mStaggeredLayoutManager.getSpanCount()];
public RecyclerViewAdapter(Context context) {
this.context = context;
final StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = staggeredGridLayoutManager.getItemCount();
lastVisibleItems = staggeredGridLayoutManager.findLastVisibleItemPositions(new int[mStaggeredLayoutManager.getSpanCount()]);
if (staggeredGridLayoutManager.getSpanCount() == 1) {
lastVisibleItem = lastVisibleItems[0];
} else if (staggeredGridLayoutManager.getSpanCount() == 2) {
lastVisibleItem = Math.max(lastVisibleItems[0], lastVisibleItems[1]);
} else if (staggeredGridLayoutManager.getSpanCount() == 3) {
lastVisibleItem = Math.max(Math.max(lastVisibleItems[0], lastVisibleItems[1]), lastVisibleItems[2]);
}
if (!isRefreshing && totalItemCount <= lastVisibleItem + visibleThreshold) {
if (mIOnLoadMoreListener != null) {
mIOnLoadMoreListener.onLoadMore();
}
isRefreshing = true;
}
}
});
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recyclerview_shop_fragment, parent, false);
return new CustomViewHolder(v);
} else if (viewType == VIEW_TYPE_LOADING) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recyclerview_loading_shop_fragment, parent, false);
return new LoadingViewHolder(v);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Product item = listItems.get(position);
if (holder.getItemViewType() == VIEW_TYPE_ITEM) {
CustomViewHolder customViewHolder = (CustomViewHolder) holder;
Glide.with(context).load(item.getFeaturedSrc()).into(customViewHolder.imageView);
} else if (holder.getItemViewType() == VIEW_TYPE_LOADING) {
if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
}
@Override
public int getItemCount() {
return listItems == null ? 0 : listItems.size();
}
@Override
public int getItemViewType(int position) {
if (listItems.get(position) == null) {
return VIEW_TYPE_LOADING;
} else {
return VIEW_TYPE_ITEM;
}
}
public void setOnLoadMoreListener(IOnLoadMoreListener mIOnLoadMoreListener) {
this.mIOnLoadMoreListener = mIOnLoadMoreListener;
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
TextView textView;
String id;
public CustomViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
imageView = (ImageView) itemView.findViewById(R.id.imageView_thumbnail);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
}
}
public class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public LoadingViewHolder(View progressView) {
super(progressView);
progressBar = (ProgressBar) progressView.findViewById(R.id.progressBar);
}
}
}
mRecyclerViewAdapter.setOnLoadMoreListener(new IOnLoadMoreListener() {
@Override
public void onLoadMore() {
listItems.add(null);
mRecyclerViewAdapter.notifyItemChanged(listItems.size() - 1);
mRecyclerViewAdapter.notifyDataSetChanged();
isProgressVisible = true;
getDataFromServer(false);
}
});
W/RecyclerView: Cannot call this method in a scroll callback. Scroll callbacks mightbe run during a measure & layout pass where you cannot change theRecyclerView data. Any method call that might change the structureof the RecyclerView or the adapter contents should be postponed tothe next frame.
java.lang.IllegalStateException:
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2581)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeChanged(RecyclerView.java:4943)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:11373)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:11364)
at android.support.v7.widget.RecyclerView$Adapter.notifyItemChanged(RecyclerView.java:6652)
at codsiga.com.xx.ShopFragment$1.onLoadMore(ShopFragment.java:148)
at codsiga.com.xx.ShopFragment$RecyclerViewAdapter$1.onScrolled(ShopFragment.java:229)
at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4618)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3679)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3323)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
03-22 11:36:32.083 3189-3189/codsiga.com.xx W/RecyclerView:
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
修改代码查看各种答案:
mRecyclerViewAdapter.setOnLoadMoreListener(new IOnLoadMoreListener() {
@Override
public void onLoadMore() {
recyclerView.post(new Runnable() {
@Override
public void run() {
listItems.add(null);
mRecyclerViewAdapter.notifyItemChanged(listItems.size() - 1);
mRecyclerViewAdapter.notifyDataSetChanged();
isProgressVisible = true;
getDataFromServer(false);
}
});
}
});
现在,我没有得到错误,但是onLoadMore()
每次都被调用两次,并且recyclerview中的视图每次都在刷新。我做错了什么?请参阅logcat输出:
D/xx: Response => {…}
D/xx: Length = 9
D/xx: Query 'SELECT * FROM products' returned 9 rows
D/xx: Response => {…}
D/xx: Length = 9
D/xx: Query 'SELECT * FROM products' returned 18 rows
无法在滚动回调中调用此方法。滚动回调可能是在度量和布局传递期间运行的,在该传递中您无法更改eCyclerView数据。任何可能更改RecyclerView结构或适配器内容的方法调用都应推迟到下一帧。
警告本身就是一种解释。换句话说,您正在更新一个视图,而recyclerview正在计算布局度量值。
哪里出了问题?以下实施-
mRecyclerViewAdapter.notifyItemChanged(listItems.size() - 1);
mRecyclerViewAdapter.notifyDataSetChanged();
recyclerView.post(new Runnable() {
public void run() {
// There is no need to use notifyDataSetChanged()
mRecyclerViewAdapter.notifyItemInserted(allArticles.size() - 1);
}
});
我正在使用notify datasetchanged,我可以解决这个问题吗?
我正在尝试创建一个事务方法,该方法调用其他几个事务方法以保存一些相互依赖的db实体。如果任何调用失败,我希望事务完全回滚。但是,这不是观察到的行为。这是我的代码: 也有and,但是当事务在第二次调用时失败时,第一个被提交。
问题内容: 有了一个有点烦人的提款机。我设法编写了以下代码,该代码从url下载JSON并将其显示在屏幕上: 但是 …这很奇怪,因为如果我想将接收到的json存储在状态对象的数据中,但是当我试图这样做时,它表示状态变量实际上不存在… 这是什么意思?由于它是 组件的WILL挂载 功能,因此状态尚不存在,所以这就是为什么我无法将接收到的数据存储在那里的原因? 有什么办法可以解决这个问题?非常感谢 PS
我正在做一个项目,在这个项目中,我必须调用recyclerview的scolling事件的api来从服务器获取一组新的问题列表。 请引导我,我会感谢你的。
我目前正在尝试为Android模拟(然后构建)和Ionic应用程序(带Angular)。我觉得有必要先说我不是Android开发者,这是我第一次不得不设置环境,所以我为任何“明显”的错误或错过的步骤道歉。 我无法克服这个错误(在运行后出现): 当我尝试构建我的项目或同步Gradle时,我在Android Studio中收到类似的消息: 我的文件中失败的行如下所示: 对于上下文,我有: 下载了And
我有一个非事务性和异步方法,它调用同一服务的其他事务性方法。 如果发生了什么事情,我会捕获一个异常,如果发生了什么事情,我会保存错误,但回滚不起作用。