正如标题在"家庭片段"上所说的时,当我单击图像瓷砖时,我希望新活动加载,但我想保持纳维巴尔可见。目前,该活动下方的代码开始,但是Navbar消失了。我想帮助编写代码,以便开始活动,但纳维尔(Navbar(留下。
public class HomeFragment extends Fragment {
private ImageView imageCbt;
private ImageView imageDistorted;
@Nullable
@Override
public android.view.View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// get the button view
imageCbt = getView().findViewById(R.id.whatIsCbt);
imageDistorted = getView().findViewById(R.id.distortedThinking);
// set a onclick listener for when the button gets clicked
imageCbt.setOnClickListener(new View.OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(),
IntroActivity2.class);
startActivity(mainIntent);
}
});
imageDistorted.setOnClickListener(new View.OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(),
TwelveTypesDistortedThinkingActivity.class);
startActivity(mainIntent);
}
});
}
要更改当前片段,您可以替换单击侦听器内的代码(用活动组中的另一个片段代替片段(:
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
然后,您需要将活动转换为片段(也许需要进行一些重构(,并更改OnClicklistener方法中的当前片段。
ps:当您使用Navbar时,片段是应用程序体系结构的最佳实践。