了解Tab活动的代码。 PagerAdapter和占位符之间是什么关系

时间:2019-05-05 20:42:29

标签: android android-fragments android-tablayout

我从AS模板创建了一个选项卡式活动。它会自动创建一些代码,包括:

ui.main文件夹:

  • PageViewModule
  • PlaceholderFragment
  • SectionsPage
  • MainActivity

java文件夹:

  • MainActivity.java

Layout.java

  • activity_mainxml
  • fragment_main.xml

我正在观看一个教学视频,它与我在AS中看到的视频有很大不同。

我了解到activity_main包含“操作”栏,其中包括选项卡布局和一个ViewPager,该ViewPager可以通过SectionsPagerAdapter.java显示片段。

我不理解的是占位符,SectionsPagerAdapter和PageViewModel之间的代码。我在代码中添加了一些注释,这是我的困惑点。

SectionPagerAdapter.java

public Fragment getItem(int position) {
        // Trying to use this to create the different Fragment but it doesn't work.
        switch(position){
            case 0:
                return new Fragment1();
            case 1:
                return new Fragment2();
            case 2:
                return new Fragment3();
        }
        return null;

        // return PlaceholderFragment.newInstance(position + 1);
    }

PlaceholderFragment.java

    private static final String ARG_SECTION_NUMBER = "section_number";
    private PageViewModel pageViewModel;

// Use PlaceholderFragment to display the Fragment selected by 
// SectionPageAdapter
    public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();

        //why we need to create a BUNDLE here?
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    // What's the function of onCreate? 
    // Why we need PageViewModel?
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
        int index = 1;
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_SECTION_NUMBER);
        }
        pageViewModel.setIndex(index);
    }

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

        // To create the Fragment. In here it is only Fragment1 
        // That's the reason why it can only display the same Fragment?
        // How to make it associate with different Fragment?
        View root = inflater.inflate(R.layout.fragment1, container, false);

        // Change the textView in the `fragment_main.xml` based on the 
        // different tab you chose. 
        // Example: Tab 1 --> textView" You selected Tab1"
        final TextView textView = root.findViewById(R.id.section_label);
        pageViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

PageViewModel.java

private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
    private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
        @Override
        public String apply(Integer input) {
            return "Hello world from section: " + input;
        }
    });

    public void setIndex(int index) {
        mIndex.setValue(index);
    }

    public LiveData<String> getText() {
        return mText;
    }

有没有人可以帮助您说明如何将Fragment与SectionsPager关联以及如何使用占位符在ViewPager中显示Fragment?这与我观看的视频几乎有80%的差异。

1 个答案:

答案 0 :(得分:0)

我知道这个迟到的答案,但如果您仍然需要答案,

要将新片段添加到sectionPager,您还需要在SectionPagerAdapter 中添加如下不同的标题,

public class SectionsPagerAdapter extends FragmentPagerAdapter {

@StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3};
private final Context mContext;

将sectionpageradapter.java中getCount中的数字更改为您的片段数,

@Override
public int getCount() {
    return 3;

然后将标签名称添加到您的strings.xml

<string name="tab_text_1">Fragment 1</string>
<string name="tab_text_2">Fragment 2</string>
<string name="tab_text_3">Fragment 3</string>

并为每个片段创建 3 个不同的类和 3 个不同的布局 xml