从RecycleView Adapter Onclick启动片段 [英] start Fragment from RecycleView Adapter Onclick
问题描述
我有一个RecycleView
Adapter
和一个按钮.我希望该按钮启动片段.我可以开始一项活动,但不能开始一段片段.我已经为我的Button
Hi I have A RecycleView
Adapter
and A button. I want that button to start a Fragment. I can start an activity but not a fragment. I have tried this Onclick
method for my Button
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putParcelable("event", events.get(getLayoutPosition()));
Fragment fragment = new EditEventDetailFragment();
fragment.setArguments(bundle);
fragment.getFragmentManager().beginTransaction().replace(R.id.contentMainDrawer,fragment).commit();
}
但是在调用null对象(contentMainDrawer
)时出错是我的主要活动content_layout.
But have error invoke null object (contentMainDrawer
) is my Main activity content_layout.
非常感谢您的帮助.片段主机回收视图是从Mainactivity调用的
Any help is much appreciate. The Fragment host recycle view is call from Mainactivity
推荐答案
我发现了2个答案,可以帮助任何需要的人.答案是使用托管调用片段的Activity.非常感谢@Mohit Suthar
Hi I have found 2 answer that help whoever needed. The answer is to use the Activity that host the calling fragment. Many thanks @Mohit Suthar
Bundle bundle = new Bundle();
bundle.putParcelable("event", events.get(getLayoutPosition()));
Fragment fragment = new EditEventDetailFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((MainActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();
这篇关于从RecycleView Adapter Onclick启动片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!