如何在点击FloatingActionButton时将数据从Activity发送到标签片段或者哪里出错,我该如何修复它,我知道已经被问过多次,我尝试所有解决方案包括this这个和this给出了明确的解释,但仍然可以工作20小时的工作,我没有错误,没有任何作用。
//主要活动中的浮动按钮
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String MyValue=" Hi fragment";
Bundle bundle = new Bundle();
bundle.putString("value", MyValue);
// set Fragmentclass Arguments
TabFragment1 fragobj = new TabFragment1();
fragobj.setArguments(bundle);
}
});
//片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab_fragment_1, container, false);
TextView textView= (TextView)view.findViewById(R.id.textView) ;
if (getArguments() != null) {
String MyValue = getArguments().getString("value");
textView.setText(MyValue);
}
return view;
}
////这里是适配器
public class PagerAdapter extends FragmentStatePagerAdapter {
private final SparseArray<WeakReference<Fragment>>
instantiatedFragments = new SparseArray<>();
private ArrayList<String> mTabHeader;
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Fragment fragment = (Fragment) super.instantiateItem(container, position);
instantiatedFragments.put(position, new WeakReference<>(fragment));
return fragment;
}
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
instantiatedFragments.remove(position);
super.destroyItem(container, position, object);
}
@Nullable
public Fragment getFragment(final int position) {
final WeakReference<Fragment> wr = instantiatedFragments.get(position);
if (wr != null) {
return wr.get();
} else {
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
return mTabHeader.get(position);
}
}
答案 0 :(得分:1)
首先,我假设您想要在按下fab时更新片段,即使您的片段已经在屏幕上。您的适配器每次都返回一个新片段,您应该保留片段实例并返回存储在数组中的给定位置的片段。 检查这个通用的FragmentStatePagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
@Override
public CharSequence getPageTitle(int position) {
if (!mFragmentTitleList.isEmpty()) {
return mFragmentTitleList.get(position);
}
return "";
}
}
然后在您的片段上创建一个更新信息的方法:
public void updateData(String string){
miTextView.setText(string);
}
最后你的活动会做这样的事情:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
YourFragment fragment = (YourFragment) adapter.getItem(desiredPosition);
fragment.updateData("your string");
}
});