2016-11-21 135 views
0

我很新的Android,学习了一下,我遇到了一个高于我的谷歌搜索技巧的问题。在片段中创建一个片段onClick

我有这样的:用工具栏和viewPager

fragment_main与按钮

主要活动

activity_main布局这一点,主要是剥离的问题:

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个片段等...

谢谢你的帮助。

+0

[我如何将一个片段添加到ViewPager? addView崩溃我的应用程序](http://stackoverflow.com/questions/16232224/how-can-i-add-a-fragment-to-a-viewpager-addview-crashes-my-app) –

+0

关键是' getCount'方法必须是动态的。让'getItem'从列表中拉出来 –

回答

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