gpt4 book ai didi

android - 从主要 Activity 访问在 Fragment 中创建的 View

转载 作者:太空狗 更新时间:2023-10-29 13:34:39 26 4
gpt4 key购买 nike

我在 fragment 中创建了一个 View

public static class GraphSectionFragment extends Fragment 
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
GraphView view = new GraphView(getActivity());
view.setGraphType(GraphView.GraphType.Oscillator);
return view;
}
}

现在我需要从主 Activity 访问我的 GraphView 对象。我如何能?我用这种方法试过,但它不起作用。

GraphSectionFragment fr = (GraphSectionFragment) m_SectionsPagerAdapter.getItem(0);
View v = fr.getView();
GraphView graph = (GraphView)v;
graph.setData(0, m_DeviceThread.getRangeDataI());

m_SectionsPagerAdapter 是继承自FragmentPagerAdapter 的类的对象。 Fragment.getView 返回一些奇怪类的 View ,而不是 GraphView。

最佳答案

您将在 this link 得到答案.您可以通过任何一种方式访问​​ Activity 中的 fragment ,具体取决于您的实现。

在扩展 FragmentActivity 的类中获取 fragment 实例:

MyclassFragment instanceFragment= (MyclassFragment)getSupportFragmentManager().findFragmentById(R.id.idFragment);

在扩展 Fragment 的类中获取 fragment 实例:

MyclassFragment instanceFragment =
(MyclassFragment)getFragmentManager().findFragmentById(R.id.idFragment);

关于android - 从主要 Activity 访问在 Fragment 中创建的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11905468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com