(滑动视图)如何在运行时为ViewPager动态制作Fragment对象?

时间:2017-04-26 10:10:00

标签: android android-fragments fragmentstatepageradapter

这个想法:在我的应用程序中,它有15节课,每节课包含30个对联用户可以轻扫。并且每个片段/ 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;}
}

2 个答案:

答案 0 :(得分:0)

尝试这种方式,在你的状态分页器适配器中使用loop-size是片段数:

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");

    }

答案 1 :(得分:0)

我会在这里回答我自己的问题

更新了片段对象代码

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;
}
}

最后主持活动

@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);
    }
}

很容易发飙!