例如在MainActivity中,我有一个简单的视图:
TextVIEw text = (TextVIEw) findvIEwByID(R.ID.tv_text);
现在我想把它传递给片段.下面的代码是我在MainActivity上的附加片段
主要活动 :
public voID attachFragment() { fts = getActivity().getFragmentManager().beginTransaction(); mFragment = new FragmentMarketDetail(); fts.replace(R.ID.cardsline,mFragment,"FragmentMarketDetail"); fts.commit();}
这是我的片段:
public class FragmentMarketDetail extends Fragment implements ObservableScrollVIEwCallbacks { public static final String SCRolLVIEW_STATE = "scrollvIEwState"; private ObservableScrollVIEw scrollVIEwTest; private Context context; private int scrollY; public static FragmentMarketDetail newInstance() { FragmentMarketDetail fragmentFirst = new FragmentMarketDetail(); return fragmentFirst; } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.fragment_online_categorIEs,container,false); scrollVIEwTest = (ObservableScrollVIEw) vIEw.findVIEwByID(R.ID.scrollVIEwTest); scrollVIEwTest.setScrollVIEwCallbacks(this); return vIEw; } @OverrIDe public voID onActivityCreated(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getActivity().getBaseContext(); }}解决方法 如何在自定义片段中添加set函数
例如
public voID setTextVIEw(TextVIEw tv){ this.tv = tv}
然后调用它
mFragment = new FragmentMarketDetail();mFragment.setTextVIEw(textVIEw)总结
以上是内存溢出为你收集整理的Android传递View作为片段的对象全部内容,希望文章能够帮你解决Android传递View作为片段的对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)