【问题标题】:findFragmentById for a viewpager, of a tab, returns the same fragment regardless of the fragment displayed选项卡的 viewpager 的 findFragmentById 返回相同的片段,而不管显示的片段
【发布时间】:2017-07-26 01:47:02
【问题描述】:

我有一个包含两个片段的选项卡。无论选择哪个选项卡,

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

单元片段:

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

【问题讨论】:

  • 分享一些你的代码,比如添加/替换片段,你在哪里调用这个findFragmentById()等等。
  • 完成,谢谢。看一看。它的行为很奇怪

标签: android android-fragments android-viewpager fragmentmanager


【解决方案1】:

尝试在您的按钮点击中使用此代码,

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

【讨论】:

  • 传奇,成功了。你能解释一下你做了什么吗?
  • ViewPager 将 Fragment 保存在 FragmentManager 中,并带有特定的标签,因此我们可以通过 FragmentManager 类通过提供标签来获取 ViewPager 的 Fragment。
  • 如果有效,请您将其标记为选定答案。 :)
  • 完成,谢谢。尽管您的确切代码给了我一个错误原因(接口)被遗漏了。 OP中的正确代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
相关资源
最近更新 更多