使用ViewPager选项卡从片段A调用片段B [英] Call Fragment B from Fragment A using ViewPager Tabs
问题描述
我做了很多程序,在单个活动中实现了多个片段,但是当使用单个活动"将多个片段作为选项卡托管,然后在Tap上显示另一个片段时,却没有...
I have done many programs, where I have implemented multiple Fragments inside a Single Activity, but not when using Single Activity to host multiple Fragments as Tabs and then on Tap show another Fragments...
使用MaterialViewPager 库,我在其中调用不同的片段以在各自的选项卡中显示视图
Using MaterialViewPager library, in which I am calling different different Fragments to show views in their respective Tabs.
就像第一个选项卡一样,我正在使用两个片段,其中
Like For the First Tab, I am using two Fragments, where
在第一片段"中,我使用RecyclerView ...显示菜单列表.
In First Fragment, I am using RecyclerView... to show list of Menus.
在第二个片段中,我使用RecyclerView ...在特定菜单下显示项目列表.
And in Second Fragment, I am using RecyclerView... to show list of Items under particular Menu.
所以这里我的问题是如何从Fragment中调用Fragment.
So here my question is How to call Fragment from Fragment.
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Value value = valueList.get(position);
List<Learning> learning = value.getLearning();
// using when putting "item" data into same recyclerview
// but on back press just exiting, not showing list of Menus again
/**
learningAdapter = new LearningAdapter(learning, R.layout.card_learning, getActivity());
mRecyclerView.setAdapter(learningAdapter);
**/
ItemFragment fragment = new ItemFragment();
replaceFragment(fragment);
}
方法replaceFragment
Method replaceFragment
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// using Fragment not Activity, so where can I use frame_container in xml
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
推荐答案
您可以拥有一个由托管这两个片段的活动实现的回调接口.片段A将使用回调来通知活动,以用片段B替换A.根据您的需要,您可以在回调方法本身中传递参数.
you can have a callback interface that is implemented by the activity which hosts these two fragments. Fragment A will use the call back to notify the activity to replace A with fragment B. Depending on your need, you can pass parameters across through the callback method itself.
您的回调界面:
public interface YourCallback {
public void methodWhichReplacesFragmentAwithB(params...);
}
您的活动托管片段:
public class YourActivity extends ... implements YourCallback {
..
..
@Override
public void methodWhichReplacesFragmentAwithB(params...) {
//insert replace fragment code here
}
}
您的片段将具有一个回调对象, YourCallback callbackObj;
.可以使用活动(从活动中作为 this
传递)本身来初始化此回调对象,因为该活动具有接口的实现.现在,您可以使用
Your fragment will have a callback object, YourCallback callbackObj;
. This callback object can be initialised using the activity (pass as this
from activity) itself since the activity has the implementation of the interface. Now, you can use
callbackObj.methodWhichReplacesFragmentAwithB(actual_params ...);
替换片段.可以利用此回调接口与父活动以及该活动中托管的其他片段进行其他通信.
to replace the fragment. This callback interface can be exploited for other communications to parent Activity as well as other fragment hosted in that activity.
这篇关于使用ViewPager选项卡从片段A调用片段B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!