了解选项卡活动的代码。寻呼机适配器和占位符之间有什么关系



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

ui.main文件夹:

  • PageViewModule
  • 占位段fragment
  • sectionspage
  • MainActivity

Java文件夹:

  • mainActivity.java

layout.java

  • activity_mainxml
  • fragment_main.xml

我正在关注一个教程视频,这与我在AS中看到的截然不同。

我理解activity_main包含一个操作栏,该操作栏包括"选项卡"布局和可以通过 SectionsPagerAdapter.java显示片段的ViewPager。

我不了解的是占位符,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);
    }

占位器fragment.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;
    }

是否有人可以帮助解释如何将碎片与sectionspager关联,以及如何使用占位符在ViewPager中显示片段?这与我观看的视频几乎不同。

我知道这个较晚的答案,但是如果您仍然需要答案,

要将新片段添加到sectionspager中,您也需要在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;

更改sectionspageradapter.java中的getCount中的数字作为您的片段

@Override
public int getCount() {
    return 3;

然后将tabe名称添加到您的字符串.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

最新更新