3

我遇到以下问题,当我滑动到第二个 viewpager 时,第一个 viewpager 错误地显示了第二个 viewpager 的内容,内容相同,正确显示了第二个 viewpager 来解决这个问题。我必须滑动到第三个,所以 FragmentStatePagerAdapter 可以重新加载,然后正确显示第一个视图页的内容。

package com.example.android.effectivenavigation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
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 android.widget.TextView;

public class MainActivity extends FragmentActivity {
    static int myViewPagerItem;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyPageAdapter mFragmentStatePagerAdapter = new MyPageAdapter(getSupportFragmentManager());
        setContentView (R.layout.refuel_viewpager);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(mFragmentStatePagerAdapter);
    }
    public static class MyPageAdapter extends FragmentStatePagerAdapter {
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int i) {
            myViewPagerItem = i;
            return new ViewFragment();
        }
        @Override
        public int getCount() {
            return 10;
        }
    }
    public static class ViewFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View v = inflater.inflate(R.layout.view, container, false);
            TextView text1 = (TextView) v.findViewById(R.id.text1);
            text1.setText("Page: " + myViewPagerItem);
            return v;
        }
    }

}
4

1 回答 1

1

这样做

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyPageAdapter mFragmentStatePagerAdapter = new MyPageAdapter(getSupportFragmentManager());
        setContentView (R.layout.refuel_viewpager);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(mFragmentStatePagerAdapter);
    }
    public  class MyPageAdapter extends FragmentStatePagerAdapter {
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
        @Override
        public Fragment getItem(int i) {
            return new ViewFragment(i);
        }
        @Override
        public int getCount() {
            return 10;
        }
    }
    public  class ViewFragment extends Fragment {
        int pageno;
        public ViewFragment(int pageNo){
            this.pageno=pageNo;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View v = inflater.inflate(R.layout.view, container, false);
            TextView text1 = (TextView) v.findViewById(R.id.text1);
            text1.setText("Page: " + pageno);
            return v;
        }
    }

}
于 2013-09-27T05:03:58.533 回答