如何在viewpager中仅更新当前片段的视图?

时间:2016-01-14 15:32:33

标签: android android-fragments android-viewpager

我在我的应用中使用了ViewPager和FragmentPagerAdapter。我创建了4个片段。我还使用EventBus库来刷新当前片段的背景颜色。当我更改页面时,触发了onEvent方法,然后更改了所有4个片段的背景颜色。但我想只改变当前片段的背景颜色。我该如何解决这个错误?对不起,我的英语不好。

ViewPager如下:

public class MyPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;

        }
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }

MyPageViewActivity如下:

 public class PageViewActivity extends FragmentActivity {
    MyPagerAdapter pageAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);



        ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
        List<Fragment> fragments = getFragments();

        pageAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
        pager.setAdapter(pageAdapter);

        pager.setOffscreenPageLimit(4);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    EventBus.getDefault.post(new TriggerEvent(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private List<Fragment> getFragments(){
        List<Fragment> fList = new ArrayList<Fragment>();

        fList.add(MyFragment.newInstance("1"));
        fList.add(MyFragment.newInstance("2"));
        fList.add(MyFragment.newInstance("3"));
        fList.add(MyFragment.newInstance("4"));

        return fList;
    }

}

最后是MyFragment.class

public class MyFragment extends Fragment {
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";


    RelativeLayout relativeLayout;
    String msg;

    public static final MyFragment newInstance(String message) {
        MyFragment f = new MyFragment();
        Bundle bdl = new Bundle(1);
        bdl.putString(EXTRA_MESSAGE, message);
        f.setArguments(bdl);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.myfragment_layout, container, false);

        msg = getArguments().getString(EXTRA_MESSAGE);
        relativeLayout = (RelativeLayout) v.findViewById(R.id.relativelayout);
        return v;
    }


    public void onEvent(TriggerEvent trigger) {


        relativeLayout.setBackgroundColor(randomColor());

    }


}

1 个答案:

答案 0 :(得分:0)

试试这个,

 public void onEvent(TriggerEvent trigger) {
        if(isCurrentFragment){
            relativeLayout.setBackgroundColor(randomColor());
        }

    }