gpt4 book ai didi

android - fragment 将 View 返回到其他 fragment

转载 作者:行者123 更新时间:2023-11-30 00:44:09 25 4
gpt4 key购买 nike

我有四个Fragment(tab swipe stuff)。例如,我在我的 Fragment #2 中创建了一个 Toast,但是当我打开它时,这个 toast 也会出现在我的 Fragment #4 中。这里缺少什么。

这是我的 fragment #2

public class A extends Fragment {
View view ;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.xxx_updates_layout, container, false);

//Custom Toast
LayoutInflater inflaterToast = getActivity().getLayoutInflater();
View layout = inflaterToast.inflate(R.layout.d_toast_d,
(ViewGroup) view.findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_people);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Ringtone Bank");
Toast toast = new Toast(getActivity().getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();


return view;
} //onCreate
}

这是我的 fragment #4

public class XXXSentFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.xxx_sent_layout,null);
}
}

我的 fragment 寻呼机适配器

public class XXXTabFragment extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4 ;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View x = inflater.inflate(R.layout.xxx_tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);


viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));



tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});

return x;

}

class MyAdapter extends FragmentPagerAdapter{

public MyAdapter(FragmentManager fm) {
super(fm);
}



@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new XXXPrimaryFragment();
case 1 : return new XXXSocialFragment();
case 2 : return new XXXUpdatesFragment();
case 3 : return new XXXFOUR();
}
return null;
}

@Override
public int getCount() {

return int_items;

}



@Override
public CharSequence getPageTitle(int position) {

switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
case 3 :
return "Four";
}
return null;
}
}

}

最佳答案

问题是 View Page 一次加载 3 个 fragment (显示的 fragment ,左侧的 fragment 和右侧的 fragment )。因此,所有 fragment 的 onCreateView 都会立即被调用,这就是出现另一个 fragment 的 toast 的原因。在所有 fragment 的 onCreateView 中放置一条日志语句来验证这一点,并更好地理解它是如何工作的。

如果您的用例是在用户滑动或选择某个 fragment 时显示 toast (在您的情况下为 XXXUpdatesFragment()),解决此问题的一种方法是在 FragmentPagerAdapter 中保存对 fragment 的引用,然后实现viewPager 上的 onPageChangeListener,当页面更改为 2 时,使用引用调用 XXXUpdatesFragment() 中的方法,该方法将显示 toast 。

public class XXXUpdatesFragment extends Fragment {
View view ;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = inflater.inflate(R.layout.xxx_updates_layout, container, false);


return view;
}

public void showToast() {
//Custom Toast
LayoutInflater inflaterToast = getActivity().getLayoutInflater();
View layout = inflaterToast.inflate(R.layout.d_toast_d,
(ViewGroup) view.findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_people);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Ringtone Bank");
Toast toast = new Toast(getActivity().getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}

关于android - fragment 将 View 返回到其他 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42083065/

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