简体   繁体   中英

Layout does not appear when replacing a fragment

I am trying to replace a fragment within a container with a different one. I made a Toast in the new fragment which is displayed but the layout does not show.

Here is where I replace the fragment:

 Bundle bundle = new Bundle();
            bundle.putString("title", textContentList.get(position).getTitle());
            bundle.putString("crated", textContentList.get(position).getCreated());
            bundle.putString("author", textContentList.get(position).getAuthor());
            bundle.putString("fullContent", textContentList.get(position).getFullContent());
            MessageDetailsFragment messageDetailsFragment = new MessageDetailsFragment();
            messageDetailsFragment.setArguments(bundle);
            replaceFragment(messageDetailsFragment);

public void replaceFragment(Fragment fragment){
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.container, fragment);
    transaction.commit();
}

Here is my fragment layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_container"
android:orientation="vertical"
android:layout_marginTop="70dp"
android:background="?android:colorBackground"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    style="@style/headings"
    android:text="Author"
    android:layout_marginTop="10dp"
    android:id="@+id/message_details_author"/>

<TextView
    style="@style/subTxt_messageDetails"
    android:text="Receiver"
    android:id="@+id/message_details_receiver"/>

<View
    android:layout_width="320dp"
    android:layout_gravity="center"
    android:layout_height="1dp"
    style="@style/dividerLine" />

<TextView
    style="@style/headings"
    android:layout_marginTop="10dp"
    android:text="Title"
    android:id="@+id/message_details_title"/>

<TextView
    style="@style/subTxt_messageDetails"
    android:text="Date"
    android:id="@+id/message_details_date"/>

<View
    android:layout_width="320dp"
    android:layout_gravity="center"
    android:layout_height="1dp"
    style="@style/dividerLine" />

<TextView
    android:text="Full content"
    style="@style/subTxt_messageDetails"
    android:layout_marginTop="15dp"
    android:id="@+id/message_details_full_content"/>

<TextView
    style="@style/subTxt_messageDetails"
    android:text="Sent by SMS"
    android:textSize="15sp"
    android:layout_marginTop="15dp"
    android:id="@+id/message_sent_by"/>

</LinearLayout>

and here is my new fragment:

public class MessageDetailsFragment extends Fragment {

private String title, created, author, fullContent;
private TextView textViewTitle, textViewDate, textViewAuthor, textViewFullContent;
private Bundle bundle;
private Context context;

public MessageDetailsFragment(){

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bundle = this.getArguments();
    title = bundle.getString("title");
    created = bundle.getString("created");
    author = bundle.getString("author");
    fullContent = bundle.getString("fullContent");
    context = getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_message_details, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    Toast.makeText(context, "ADDASD", Toast.LENGTH_SHORT).show();

    textViewTitle = (TextView) getView().findViewById(R.id.message_details_title);
    textViewTitle.setText(title);
    textViewDate = (TextView) getView().findViewById(R.id.message_details_date);
    textViewDate.setText(created);
    textViewAuthor = (TextView) getView().findViewById(R.id.message_details_author);
    textViewAuthor.setText(author);
    textViewFullContent = (TextView) getView().findViewById(R.id.message_details_full_content);
    textViewFullContent.setText(fullContent);

}
}

Your sample is not reproducible as it refers to styles etc not given so it is difficult to say for sure so generally speaking....

To debug a Fragment layout, first clear it of everything except the parent ViewGroup. Give it an easily identifiable colour so you can see that it is actually appearing in your app when you initiate it.

If it appears, the problem is your layout. If not, it is most likely your code.

Once you have done this, you can gradually add your other views.

eg

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="@color/Red"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 </LinearLayout

Thanks for all the answers. Turns out that I tried to insert the fragment into a viewpager which I also use in the application. By creating a new FrameLayout and inserting it into that I resolved my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM