【问题标题】:fragment doesn't show in framelayout in linearlayout片段不显示在线性布局的框架布局中
【发布时间】:2018-03-13 20:53:18
【问题描述】:

我使用片段在一个活动中显示多个视图。我在一个线性布局中有 2 个框架布局。片段的 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));
    }
}

在activity的onCreate中添加fragment的代码:

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

提前致谢!

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    您的第一个布局 standardFlowDownloadContainer 的宽度和高度是 match_parent ,所以 standardFlowBaseContainer FrameLayout 不在屏幕上。您可以将 standardFlowDownloadContainer 的高度更改为 20dp 并运行您的项目,您将看到您的 Fragment 内容。

    【讨论】:

    • 非常感谢,确实是这个问题。
    猜你喜欢
    • 2019-12-04
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多