【发布时间】:2019-03-03 12:32:07
【问题描述】:
我正在开发一个类似于“主页”的功能,其中有一个主要的Fragment,并在其布局中添加了几个其他片段,构成了内容页面。我希望主片段类能够实例化从某个父片段类继承的所有片段类。这样代码会更加动态,而不是在我的 xml 文件中添加一堆 <fragment> 标签。
我有点坚持构建一个体面的架构。您将如何继续这样做?
更新: 这就是我基本上想要做的,但不知道如何做:
public class FeedFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View parentView = inflater.inflate(R.layout.fragment_home, container, false);
// Get fragments and dynamically add them to
// the FeedFragment's layout
getEntryFragmentsList();
// ...
return parentView;
}
}
public abstract class FeedEntryFragment extends Fragment {
// Somehow add fragment to list of entry fragments
}
public class TestFragment extends FeedEntryFragment {
// Already added to list of entry fragments
}
【问题讨论】:
-
请贴一些代码。
标签: java android android-fragments architecture