在片段内部创建一个片段onClick

时间:2016-11-21 13:24:16

标签: java android android-fragments android-viewpager swipeview

我对android非常陌生,学习了一点,我在google搜索技能之上遇到了一个问题。

我有这个:

包含工具栏和viewPager的activity_main布局

fragment_main with button

主要活动是这个,主要是剥离问题:

public class MainActivity extends AppCompatActivity {


private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);


}

/**
 * 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";
    private FragmentManager supportFragmentManager;
    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;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        //TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        Button newFragmentBtn = (Button) rootView.findViewById(R.id.cameraBtn);
        newFragmentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             // create new fragment with same layout on right so I can swipe to it 


            }

        });
        return rootView;
    }

    public FragmentManager getSupportFragmentManager() {
        return supportFragmentManager;
    }
}



/**
 * returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);

    }

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


}

}

所以我现在有3个片段(可能是任何数字),但是我希望这不是固定数字,而是当我点击片段内的按钮时增加,这样当我点击在newFragmentBtn上,它会创建4个片段进行滑动,再次点击,5个片段等...

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

将字段fragCount添加到SectionsPagerAdapter(应扩展FragmentStatePagerAdapter),添加setter以更改所需的片段数,并且在更改计数器后不要忘记调用notifyDataSetChanged():

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    private int fragCount = 3;//initial number
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position);
    }

    public void setCount(int fragCount){
        this.fragCount = fragCount;
        notifyDataSetChanged();
    }

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