(Swipe Views) 如何在运行时为 ViewPager 动态制作 Fragment 对象?

(Swipe Views) how to make Fragment objects dynamically for ViewPager at runtime?

想法:在我的应用程序中,它有 15 课,每课包含 30 句对联,用户可以轻扫浏览。每个 fragment/couplet_page 都膨胀了一个包含 3 个文本视图的布局。我正在使用 FragmentStatePagerAdapter。

我知道使用 switch case 语句来创建滑动视图,但问题是,有很多片段要创建,我知道有更好的解决方案,但我不知道。我只想使用一个片段并以某种方式使用 setText 方法更改文本。

这是我的一个片段对象代码

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CoupletOneFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.couplet_one, container, false);
    }
}

这是自定义寻呼机适配器

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


public class MyFragmentAdapter extends FragmentStatePagerAdapter {

    public MyFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
       switch (position) {
           case 0:
               return new CoupletOneFragment();
           case 1:
               return new CoupletTwoFragment();
           case 2:
               return new CoupletThreeFragment();
           case 3:
               return new CoupletFourFragment();
           case 4:
               return new CoupletFiveFragment();
and so on...
           default:
               break;
       }
       return null;
    }

    @Override
    public int getCount() {return 30;}
}

尝试这种方式,在您的状态寻呼机适配器中使用循环-大小是片段数:

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        for (int i=0;i<size;i++){

            adapter.addFragment(CoupletFiveFragment.newInstance(i));
        }

        viewPager.setAdapter(adapter);

适配器:

public  class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment) {
            mFragmentList.add(fragment);

        }


    }

现在你的片段:

public static CoupletFiveFragment newInstance(int j) {
        CoupletFiveFragment fragmentDemo = new CoupletFiveFragment();
        Bundle args = new Bundle();
        args.putInt("GET_LIST", j);
        fragmentDemo.setArguments(args);
        return fragmentDemo;
    }

在 Oncreate 中获取价值:

Int getINT=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getINT = getArguments().getInt("GET_LIST");

    }

好吧,我要在这里回答我自己的问题

更新片段对象代码

public class FragmentChild extends Fragment {

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

    // INFLATE THE LAYOUT THAT EACH FRAGMENT OBJECT WILL HAVE, PUT IT IN A VIEW
     View root = inflater.inflate(R.layout.couplets, container, false);

    // RECEIVE THE BUNDLE DATA SENT (ARGUMENTS)
    Bundle args = getArguments();

    // CREATE AN ARRAY LIST OF STRINGS THAT WILL HOLD TEXT
    ArrayList<String> someText = new ArrayList<>();
someText.add("one");
someText.add("two");
someText.add("three");
TextView txt = (TextView) root.findViewById(R.id.text_view);
    txt.setText(someText.get(args.getInt("position")));
return root;
}

更新了自定义寻呼机适配器

class MyFragmentAdapter extends FragmentStatePagerAdapter {

 MyFragmentAdapter(FragmentManager fm) {super(fm);}

@Override
public Fragment getItem(int position) {
    Bundle args = new Bundle();
    args.putInt("position", position);

    Fragment fragment = new FragmentChild();
    fragment.setArguments(args);
    return fragment;
}

@Override
public int getCount() {
    return 3;
}
}

最后主持人 activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chapter_one);

    // FIND THE VIEWPAGER AND SET THE CUSTOM ADAPTER TO IT TO PROVIDE CHILD PAGES
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }
}

简单易行!