简体   繁体   中英

fragment doesn't show in framelayout in linearlayout

I use fragments to display multiple views in one activity. I have 2 framelayouts in one linearlayout. The onCreate and onCreateView of the fragment gets called but the view of the fragment is not displayed. Is what i'm trying to do not possible? Or is there a way to fix issue?

Layout of the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".view.activity.StandardFlowActivity">

    <FrameLayout
        android:id="@+id/standardFlowDownloadContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/standardFlowBaseContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

Layout of the fragment

<FrameLayout 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"
tools:context="com.example.sennevervaecke.crossexperience.view.fragment.WedstrijdFragment">
<ListView
    android:id="@+id/wedstrijdListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</FrameLayout>

Fragment Class

public class WedstrijdFragment extends Fragment implements AdapterView.OnItemClickListener{

    private ArrayList<Wedstrijd> wedstrijden;
    private WedstrijdFragmentCom communication;

    public WedstrijdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("wedstrijdFragment", "onCreate is called");
        super.onCreate(savedInstanceState);
        wedstrijden = LocalDB.getWedstrijden();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("wedstrijdFragment", "onCreateView is called");

        View view = inflater.inflate(R.layout.fragment_wedstrijd, container, false);
        ListView listView = view.findViewById(R.id.wedstrijdListView);
        WedstrijdAdapter adapter = new WedstrijdAdapter(getContext(), wedstrijden);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            communication = (WedstrijdFragmentCom) activity;
        } catch (ClassCastException e){
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        communication.onWedstrijdItemClick(wedstrijden.get(i));
    }
}

Code in the onCreate of the activity to add the fragment:

wedstrijdFragment = new WedstrijdFragment();
getSupportFragmentManager().beginTransaction().add(R.id.standardFlowBaseContainer, wedstrijdFragment, "wedstrijd").commit();

Thanks in advance!

Your first layout standardFlowDownloadContainer width and height is match_parent , so the standardFlowBaseContainer FrameLayout is out of the screen. you can change your standardFlowDownloadContainer 's height to 20dp and run your project , you will see your Fragment content.

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