I have a certain Fragment , which contains a ScrollView , and inside it there is a LinearLayout , to which outgoing and incoming messages are added:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="fill" android:background="#D1EDF6"> <TextView android:text="" android:layout_width="match_parent" android:layout_height="0dp" android:gravity="center_horizontal|top" android:id="@+id/tvChatterName" android:layout_alignParentTop="true" android:textColor="#000"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tvChatterName" android:layout_above="@+id/llChatBottom"> <LinearLayout android:orientation="vertical" android:id="@+id/chatListLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> </ScrollView> The Fragment itself is a class that first reads the message history, displays it on the screen, and after that starts IntentService , which constantly refers to the database and sends a message to the BroadcastReceiver if there are new ones. I catch it and bring new messages to the screen.
When you start the fragment for the first time, everything works perfectly, messages are displayed and correctly displayed. Then I go out and re-enter the chat, replacing the snippet as follows:
public void openFragment(Fragment fragment){ this.fragment=fragment; FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit(); } Replacement occurs correctly, the chat opens, reads the message history, displays it on the screen. After that, the service starts again, but when a new message arrives, although it is correctly processed (in the same way as creating a message when reading a story), adding it to LinearLayout does not show it on the screen.
Checking LayoutParams showed that they are the same as the correct messages. In LinearLayout itself LinearLayout new messages are displayed, along with all the parameters. Creating a new View carried out through LayoutInflater , I suspect that the problem may be in it, but I do not see what. Now I do inflate like this:
if(getActivity() != null) { LayoutInflater inflater = LayoutInflater.from(getActivity()); sendView = inflater.inflate(R.layout.send, null); sendContent = (TextView) sendView.findViewById(R.id.sendContentText); sendTitle = (TextView) sendView.findViewById(R.id.sendTitleText); sendDate = (TextView) sendView.findViewById(R.id.sendDateText); sendAvatar = (ImageView) sendView.findViewById(R.id.sendAvatarImage); sendTitle.setTextSize(10); sendContent.setTextSize(9); sendDate.setTextSize(9); } I onCreateView() these views to this Layout , defined in onCreateView() :
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_chat, container, false); chatListLayout = (LinearLayout) rootView.findViewById(R.id.chatListLayout); I tried to do inflate for sendView , specifying the Layout parameter as the parent :
sendView = inflater.inflate(R.layout.send, chatListLayout, null); Also tried differently to get LinearInflater , but the result remains the same.
Adding to the Layout also conducted in several ways, through a simple addView() and addView() , clearly indicating LayoutParams , but this did not have any effect.
I hope someone faced this problem and will be able to help understand what is happening such that the views are lost in the void.