For calling API after creating the activity, I need to perform click event for RecyclerView item of " 0 " index.
for that in onBindviewHolder I'm calling holder.view.performClick event. But the Problem is I need to call a notifydataSetChanged() method in onClick event to update the adapter. Initially, it will auto click on position "0" and calling notifydataSetChanged() method and crashing. What is the solution for this problem? kindly help.
public void onBindViewHolder(ViewHolder holder, int position) {
if (position==0){
holder.itemView.performClick();
}
I'm calling this interface in MainActivity.class
public void onRecyclerViewItemClick(@NonNull View view, @NonNull CalendarDTO calendarDTO, int position) {
isClickedPosition=position;
adapter.notifyDataSetChanged();
Toast.makeText(this, calendarDTO.getDate(), Toast.LENGTH_SHORT).show();
}
please check the error:-
FATAL EXCEPTION: main
Process: in.playin.calendarrecyclerview, PID: 4063
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2575)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:4932)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11359)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6636)
at in.playin.calendarrecyclerview.CalendarRecyclerViewAdapter$ViewHolder$1.onClick(CalendarRecyclerViewAdapter.java:115)
at android.view.View.performClick(View.java:5207)
at in.playin.calendarrecyclerview.CalendarRecyclerViewAdapter.onBindViewHolder(CalendarRecyclerViewAdapter.java:59)
at in.playin.calendarrecyclerview.CalendarRecyclerViewAdapter.onBindViewHolder(CalendarRecyclerViewAdapter.java:22)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
at android.view.View.measure(View.java:18809)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18809)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18809)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:18809)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18809)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18809)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5955)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18809)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
Try This
In OnBindViewHolder
public static boolean isPerformClicked;
int performClickCount;
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
if (i==0){
viewHolder.tv_android.setBackgroundColor(Color.RED);
if (performClickCount==0){
isPerformClicked = true;
viewHolder.itemView.performClick();
}else{
viewHolder.tv_android.setBackgroundColor(Color.RED);
}
}else{
isPerformClicked=false;
performClickCount = performClickCount+1;
viewHolder.tv_android.setBackgroundColor(Color.YELLOW);
}
if (i!=BlankFragment.selectedPosition){
viewHolder.tv_android.setBackgroundColor(Color.YELLOW);
}else{
viewHolder.tv_android.setBackgroundColor(Color.RED);
}
viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
}
In holder Item Click or item
@Override
public void onRecyclerViewItemClick(@NonNull View view, @NonNull AndroidVersion calendarDTO, int position) {
selectedPosition=position;
Toast.makeText(getContext(),String.valueOf(position), Toast.LENGTH_SHORT).show();
if (!isPerformClicked){
adapter.notifyDataSetChanged();
}
}