片段不会在linearlayout中的framelayout中显示

时间:2018-03-13 12:45:35

标签: java android android-fragments

我使用片段在一个活动中显示多个视图。我在一个linearlayout中有2个framelayout。调用片段的onCreate和onCreateView,但不显示片段的视图。我想做的不可能吗?或者有办法解决问题吗?

活动布局:

<?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>

片段的布局

<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>

片段类

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));
    }
}

添加片段的活动的onCreate中的代码:

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

您的第一个布局standardFlowDownloadContainer宽度和高度为match_parent,因此standardFlowBaseContainer FrameLayout不在屏幕范围内。您可以将standardFlowDownloadContainer的高度更改为20dp并运行项目,您将看到片段内容。