其他分享
首页 > 其他分享> > android-选项卡的viewpager的findFragmentById,无论显示的片段如何,都返回相同的片段

android-选项卡的viewpager的findFragmentById,无论显示的片段如何,都返回相同的片段

作者:互联网

我有一个带有两个片段的标签.无论选择哪个标签,

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());

解决方法:

尝试在单击按钮时使用此代码,

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);
    }
});

标签:fragmentmanager,android-fragments,android-viewpager,android
来源: https://codeday.me/bug/20191111/2017629.html