There is recyclerview in fragments. Fragments of turn in tabs. They display the history of correspondence. When I want to send a message, it adds an item ONLY to 1 tab, and at 2 it does not work ((how to fix? Code when you click the send button:

btn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Message message = null; message = new Message("text", new Date(), finalDoctor.getName() + " " + finalDoctor.getSurname(), tab.getId()); list.add(message); adapter.swapItems(list); 

Adapter for messages:

  public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder>{ private List<Message> list; private Context context; private SimpleDateFormat fmt; public MessageAdapter(Context context, List<Message> body) { this.context = context; this.list = body; this.fmt = new SimpleDateFormat("dd.MM.yyyy HH:mm"); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(context); View contactView = inflater.inflate(R.layout.item_message, parent, false); return new ViewHolder(contactView); } @Override public void onBindViewHolder(ViewHolder vh, int position) { vh.text.setText(list.get(position).getText()); vh.date.setText(fmt.format(list.get(position).getCreationDate())); vh.sender.setText(context.getResources().getString(R.string.sender)+" "+list.get(position).getSender()); } @Override public int getItemCount() { return list.size(); } public void swapItems(List<Message> items) { this.list = items; Log.e("samuliak", "list size from adapter > " + list.size()); notifyItemInserted(list.size()); //notifyDataSetChanged(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView text, date, sender; public ViewHolder(View itemView) { super(itemView); text = (TextView) itemView.findViewById(R.id.mes_text); date = (TextView) itemView.findViewById(R.id.mes_date); sender = (TextView) itemView.findViewById(R.id.mes_sender); } } } 

Here is the screen itself, for clarity (a check mark is where it works. A cross is where it does not work):

enter image description here

UPDATE

Here is a stack of errors:

 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{a171d84 position=3 id=-1, oldPos=2, pLpos:2 scrap [attachedScrap] tmpDetached no parent} at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4505) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4636) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2979) at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2619) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17547) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1560) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668) at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1170) at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615) at android.view.View.measure(View.java:17547) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographe 

UPDATE # 2

It is noticed that if the method in the adapter is changed like this:

 public void swapItems(List<Message> items) { this.list = items; Log.e("samuliak", "list size from adapter > " + list.size()); ..notifyItemInserted(list.size()); notifyDataSetChanged(); } 

then there will be no error, + adds elements to the end, but displays everything in the tab in which the first click occurred. And in the rest unchanged.

  • show the adapter completely - Kirill Stoianov
  • Added message adapter - Samuliak
  • one
    You brought the text of the adapter for RecyclerView, for some I understand for one taba, in which there are changes, it turns out that other tabs have their own RecyclerView with their adapters? Those. If two tabs mean 2 RecyclerView in them and two adapters to them? - Kirill Stoianov
  • It turns out that it is! Thank you for imbued with the problem!) I will implement everything differently, which would be easier. - Samuliak
  • all you need is to do it too, what you do only in all tabahj - Kirill Stoianov

1 answer 1

If your tabs are fragments switching between which is implemented using an adapter, you need to take a list of tabs (fragments, if you use them) from the TabAdapter and on each such fragment call the method that updates the data.