我有一个带有两个片段的标签。无论选择哪个选项卡,
getSupportFragmentManager().findFragmentById(R.id.container)
每次都会在第二个标签中返回片段的ID。
我试验并添加了另一个标签,重复其中一个片段。奇怪的行为。单击第一个片段上的按钮会更改第二个片段,然后单击第三个片段会更改第二个片段,然后单击第二个片段会更改第三个片段。但是然后在所有选项卡之间滑动只会重置第一个和第三个片段。
知道如何解决或解决这个问题吗?
的活动:
public class MainActivity extends AppCompatActivity {
//The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the sections.
private SectionsPagerAdapter mSectionsPagerAdapter;
public android.support.v4.app.Fragment test;
//The {@link ViewPager} that will host the section contents.
private ViewPager mViewPager;
private ActivityMainBinding binding;
public interface Interface {
public void InterfaceMethod(int number);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
//Test button1 click behaviour
binding.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Interface interfaceUse = (Interface)(getSupportFragmentManager().findFragmentById(R.id.container));
interfaceUse.InterfaceMethod(1);
}
});
}
protected void buttonClick(int a, int b){
int position = binding.tabs.getSelectedTabPosition();
}
//A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the sections/tabs/pages.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int index)
{
switch (index)
{
case 0:
return new UnitFragment();
case 1:
return new RmFragment();
case 2:
return new UnitFragment();
}
return null;
}
@Override
public int getCount() {
// Show 2 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Unit Converter";
case 1:
return "One Rep Max";
case 2:
return "Dum";
}
return null;
}
}
}
UnitFragment:
public class UnitFragment extends Fragment implements MainActivity.Interface{
private TextView textview1;
public void InterfaceMethod(int position) {
textview1.setText(Integer.toString(position));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.unit_fragment, container, false);
//r.id
textview1 = (TextView) rootView.findViewById(R.id.textView);
return rootView;
}
}
修正:
Interface interfaceUse =(Interface) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.container + ":" + mViewPager.getCurrentItem());
答案 0 :(得分:3)
尝试在按钮单击中使用此代码,
binding.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Interface interfaceUse = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.container + ":" + mViewPager.getCurrentItem());
interfaceUse.InterfaceMethod(1);
}
});