Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android Studio中编辑片段内容?_Java_Android_Xml_Android Studio - Fatal编程技术网

Java 如何在Android Studio中编辑片段内容?

Java 如何在Android Studio中编辑片段内容?,java,android,xml,android-studio,Java,Android,Xml,Android Studio,我一直在Android Studio中努力创建一个ViewPager,但我刚刚意识到,您可以使用预先配置的活动自动创建一个 使用ViewPager创建了一个选项卡式活动:操作栏选项卡,最后我得到了两个XML布局:Activity\u main.XML和fragment\u main.XML。但是,我不知道如何独立地更改每个片段。每当我在fragment_main.xml中添加TextView时,它就会出现在所有三个预定义的部分上 如何在不影响其他两个部分的情况下更改一个部分 在FragmentP

我一直在Android Studio中努力创建一个
ViewPager
,但我刚刚意识到,您可以使用预先配置的
活动自动创建一个

使用
ViewPager
创建了一个
选项卡式活动
操作栏选项卡
,最后我得到了两个XML布局:
Activity\u main.XML
fragment\u main.XML
。但是,我不知道如何独立地更改每个片段。每当我在
fragment_main.xml
中添加
TextView
时,它就会出现在所有三个预定义的部分上

如何在不影响其他两个部分的情况下更改一个部分

在FragmentPagerAdapter中的方法getItem(int position)中,这一行占位符fragment.newInstance(position+1)决定返回哪个片段。 只有一种类型的片段,返回的片段具有相同的布局,但在占位符片段中,您可以根据位置更改其布局,例如:

 /**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }


    private int mPosition;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPosition = getArguments().getInt(ARG_SECTION_NUMBER);

    }
    private View mRootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        switch (mPosition) {
            case 0:
                 mRootView = inflater.inflate(R.layout.fragment_layout_0, container, false);
                break;
            case 1:
                 mRootView = inflater.inflate(R.layout.fragment_layout_1, container, false);
                break;
            case 2:
               mRootView = inflater.inflate(R.layout.fragment_layout_2, container, false);
                break;
        }
        TextView textView = (TextView) mRootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return mRootView;
    }
}
在FragmentPagerAdapter中的方法getItem(int位置)中,这一行占位符fragment.newInstance(位置+1)决定返回哪个片段。 只有一种类型的片段,返回的片段具有相同的布局,但在占位符片段中,您可以根据位置更改其布局,例如:

 /**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }


    private int mPosition;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPosition = getArguments().getInt(ARG_SECTION_NUMBER);

    }
    private View mRootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        switch (mPosition) {
            case 0:
                 mRootView = inflater.inflate(R.layout.fragment_layout_0, container, false);
                break;
            case 1:
                 mRootView = inflater.inflate(R.layout.fragment_layout_1, container, false);
                break;
            case 2:
               mRootView = inflater.inflate(R.layout.fragment_layout_2, container, false);
                break;
        }
        TextView textView = (TextView) mRootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return mRootView;
    }
}

你应该读书。这将向您展示如何使用
ViewPager
创建选项卡。特别是,您需要一个
PagerAdapter
,它负责为每个选项卡创建正确的片段。

您应该阅读。这将向您展示如何使用
ViewPager
创建选项卡。特别是,您需要一个
PagerAdapter
,它负责为每个选项卡创建正确的片段。

记住,所有Java代码都必须在一个类中。另外,使用
PagerAdapter
和多个
片段
子类最合适,每个布局一个。请记住,所有Java代码都必须在一个类中。此外,最好使用一个
PagerAdapter
和多个
Fragment
子类,每个布局一个。