我正在尝试创建一个活动来管理Fragments
中ViewPager
的动态创建...问题是我在管理它们方面遇到了问题。
Fragments
应该在活动开始时创建,如果我向左或向右滑动,它将不会再创建它们,但会向我显示正确的。{/ p>
package info.androidhive.materialdesign.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import info.androidhive.materialdesign.R;
import info.androidhive.materialdesign.utility.MyDataContainer;
import xdroid.toaster.Toaster;
public class ScreenSlidePagerActivity extends Fragment {
/**
* The number of pages (wizard steps) to show in this demo.
*/
public int NUM_PAGES = MyDataContainer.getNumberOfStreets(MyDataContainer.allStreets) + 1;
static int mPageLastScreen=0;
static int currentPage = 0 ;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
private List<Fragment> fragments = new ArrayList<Fragment>();
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private ViewPagerAdapter mPagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_screen_slide);
for(int x = 0; x < NUM_PAGES ; x++) {
fragments.add(new ScreenSlidePageFragment());
}
}
public void onResume(){
super.onResume();
if (!(mPagerAdapter == null)) {
mPagerAdapter.notifyDataSetChanged();
}
}
public void onViewCreated(View v,Bundle savedInstanceState){
super.onViewCreated(v, savedInstanceState);
mPager = (ViewPager) v.findViewById(R.id.pager);
mPagerAdapter = new ViewPagerAdapter(getFragmentManager(),fragments);
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
private float thresholdOffset = 0.5f;
private boolean firstPlus = true;
private boolean firstMinus = true;
public void onPageScrollStateChanged(int state)
{}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
if(mPageLastScreen!=position)
{
if (thresholdOffset > positionOffset) {
Toaster.toast("Page has been Scrolled left " + currentPage);
if (firstPlus){
firstPlus = false;
}
else {
currentPage++;
firstMinus = true;
}
MyDataContainer.currentView = currentPage;
Toaster.toast("currentView " + MyDataContainer.currentView);
} else {
Toaster.toast("Page has been Scrolled right, current page : " + currentPage );
if (firstMinus){
firstMinus = false;
}
else {
currentPage--;
firstPlus = true;
}
MyDataContainer.currentView = currentPage;
Toaster.toast("currentView " + MyDataContainer.currentView);
}
mPageLastScreen=position;
}
}
public void onPageSelected(int position)
{}
});
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> fragments;
/**
* @param fm
* @param fragments
*/
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size() - 1;
}
@Override
public int getItemPosition(Object object) {
Toaster.toast("I want number : " + currentPage + " " + MyDataContainer.currentView);
return MyDataContainer.currentView;
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_screen_slide, container, false);
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
此代码一次只创建3个片段,当您滑动到其他片段时,它将删除之前加载的三个片段中的一个并添加它刚刚创建的片段。
以下是我的代码:
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
if(mPageLastScreen!=position)
{
if (thresholdOffset > positionOffset) {
Toaster.toast("Page has been Scrolled left " + currentPage);
if (firstPlus){
firstPlus = false;
}
else {
currentPage++;
firstMinus = true;
}
MyDataContainer.currentView = currentPage;
Toaster.toast("currentView " + MyDataContainer.currentView);
} else {
Toaster.toast("Page has been Scrolled right, current page : " + currentPage );
if (firstMinus){
firstMinus = false;
}
else {
currentPage--;
firstPlus = true;
}
MyDataContainer.currentView = currentPage;
Toaster.toast("currentView " + MyDataContainer.currentView);
}
mPageLastScreen=position;
}
}
这段代码设置了你正在寻找的当前片段。