如何在 PagerAdapter 中返回多个片段(不是 FragmentPagerAdapter)

Posted

技术标签:

【中文标题】如何在 PagerAdapter 中返回多个片段(不是 FragmentPagerAdapter)【英文标题】:How to return multiple fragments in PagerAdapter(not FragmentPagerAdapter) 【发布时间】:2021-09-13 05:13:45 【问题描述】:

PagerAdapter 类

我正在构建一个具有许多功能的应用程序。我创建了一个选项卡布局。有两个片段。如何在 instantiateItem 方法中返回片段。我在输出 EmployeeAdd 片段中做强文本的方式没有显示任何内容。之前我创建了 FragmentPagerAdapter。代码工作正常。但是由于 FragmentPagerAdapter 已弃用,所以我切换到 PagerAdapter

public class SectionsPagerAdapter extends PagerAdapter 

    @StringRes
    private static final int[] TAB_TITLES = new int[]R.string.tab_text_1, R.string.tab_text_2;
    private final Context context;
    private LayoutInflater layoutInflater;

    public SectionsPagerAdapter(Context context) 
        this.context = context;
    

    int layouts [] = R.layout.fragment_employee_add, R.layout.fragment_employee_delete;

    @Override
    public int getCount() 
        return layouts.length;
    

    @Override
    public Object instantiateItem(ViewGroup container, int position)
        layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View one=layoutInflater.inflate(R.layout.fragment_employee_add,container,false);
        View two=layoutInflater.inflate(R.layout.fragment_employee_delete,container,false);
        View viewArray [] = one, two;
        container.addView(viewArray[position]);

        switch (position)
            case 0:
                return new EmployeeAddFragment();
            case 1:
                return new EmployeeDeleteFragment();
        
        //container.addView(one);
        return container;
    

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) 
        return context.getResources().getString(TAB_TITLES[position]);
    
    @Override
    public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) 
        return view == object;
    

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) 
        collection.removeView((View) view);
    

EmployeeAddFragment

这是我的片段。它有许多 editText 和一个将所有数据添加到数据库的按钮。我无法解决问题。请帮帮我

public class EmployeeAddFragment extends Fragment 

    DBHelper myDB;

    EditText substationNumber, employeeId, employeeName, password, phoneNumber, dateOfJoining, permanentOrTemporary, designation, security;
    Button addEmployee;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
        View rootView = inflater.inflate(R.layout.fragment_employee_add, container, false);

        substationNumber = rootView.findViewById(R.id.substationNumber);
        employeeId = rootView.findViewById(R.id.employeeId);
        employeeName = rootView.findViewById(R.id.employeeName);
        password = rootView.findViewById(R.id.passwordEmployee);
        phoneNumber = rootView.findViewById(R.id.phoneNumber);
        dateOfJoining = rootView.findViewById(R.id.dateOfJoining);
        permanentOrTemporary = rootView.findViewById(R.id.permanentOrTemporary);
        designation = rootView.findViewById(R.id.designation);
        security = rootView.findViewById(R.id.security);
        addEmployee = rootView.findViewById(R.id.addSubstationButton);

        if(myDB == null)
            myDB = new DBHelper(getActivity());

        addEmployee.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                addEmployeeFunction();
            

        );

        return rootView;
    

    //function to add user to the database by admin
    private void addEmployeeFunction() 

        //Todo edit this part
        Toast.makeText(getActivity().getApplicationContext(), "Employee Added Successfully", Toast.LENGTH_LONG).show();

        /*String getEmployeeID = employeeId.getText().toString();
        String getEmployeeName = employeeName.getText().toString();
        String getPassword = password.getText().toString();

        int lengthOfPassword = password.length();

        if(TextUtils.isEmpty(getEmployeeID) || TextUtils.isEmpty(getEmployeeName) ||TextUtils.isEmpty(getPassword) )
            Toast.makeText(getActivity().getApplicationContext(), "Please enter all the fields.", Toast.LENGTH_LONG).show();
        else if(lengthOfPassword < 6)
            Toast.makeText(getActivity().getApplicationContext(), "Password length must be greater than 5", Toast.LENGTH_LONG).show();
        else
            boolean isInserted = myDB.insertDataIntoDatabase(getEmployeeID, getEmployeeName, getPassword);
            if(isInserted)
                Toast.makeText(getActivity().getApplicationContext(), "Employee Added Successfully", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getActivity().getApplicationContext(), "Entry failed.", Toast.LENGTH_LONG).show();

            employeeId.setText("");
            employeeName.setText("");
            password.setText("");
        */
    

【问题讨论】:

【参考方案1】:

您可以使用 SmartFragmentStatePagerAdapter https://guides.codepath.com/android/viewpager-with-fragmentpageradapter

【讨论】:

以上是关于如何在 PagerAdapter 中返回多个片段(不是 FragmentPagerAdapter)的主要内容,如果未能解决你的问题,请参考以下文章

滑动 ViewPager 时如何清除片段相关状态

如何从 FragmentPagerAdapter 返回多个片段

在使用 PagerAdapter 和 ViewPager 中的选项卡之间滑动时出现问题

如何在ViewPager中的各个Fragment间传递数据并刷新Fragment界面

从另一个活动返回后如何恢复活动中最后打开的片段

如何在按下单个片段的手动后退按钮时返回上一个片段?