Changing background image of current fragment in viewpager

江枫思渺然 提交于 2019-12-04 09:58:41

Solution:--

actually your problem is you are recycling bitmap i.e you are freeing that bitmap and the pixel data so, obviously you will get the null .!

It is already mentioned in android api documents:--

You just need to do this:--

int i = pager.getCurrentItem();
Fragment page = ((MyPagerAdapter)pager.getAdapter()).getRegisteredFragment(i);
Bitmap bitmapNeedTobeRecycled = ((BitmapDrawable)page.getView().getBackground()).getBitmap()
page.getView().setBackgroundResource(R.drawable.wall3_nopaperball);
bitmapNeedTobeRecycled.recycle();

It works then..Enjoy..!

First of all do not keep a list of fragments in your adapter. Pager adapters should be served with data through custom method and then your getItem() should use it to create Fragments. Instead randomising layouts in getItem() do it on your item list(see code below) before you pass it to adapter. Do not override initialiseItem() or destroyItem(). Also you do not have to keep reference to FragmentManager. You can implement your adapter like this:

Create custom class to holds your data.

public class MyItem {
    private int layout;
    private int bitmap;

    public MyItem(layout, bitmap) {
        this.layout = layout;
        this.bitmap = bitmap;
    }

    //implement getters and setters
}

Next implement adapter like this:

public static class MyPagerAdapter extends FragmentStatePagerAdapter {
    private List<MyItem> items = new ArrayList<MyItem>(0);

    public void setItems(List<MyItem> items) {
        this.items = items;
    }

    @Override
    public Fragment getItem(int pos) {
        MyItem item = items.get(pos);
        return FirstFragment.newInstance(item.getLayout(), item.getBitmap());
    }

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

    public void updateItemBitmap(int position, int bitmap) {
        MyItem myItem = items.get(position);
        myItem.setBitmap(bitmap);
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

Then in getItem() method you initialise your fragments based on that data. Next create method for updating items which is updating provided position and then invalidate adapter. Don't forget to implement getItemPosition(), it makes ViewPager to invalidate all items, also those which are already created.

Finally, you can update it in your activity like this:

int i = pager.getCurrentItem();
((MyPagerAdapter)pager.getAdapter()).updateItemBitmap(i,R.drawable.wall3_nopaperball);

Your fragment implementation would look like this:

public class FirstFragment extends Fragment{

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            int layout = getArguments().getInt("layout");
            int bitmap = getArguments().getInt("bitmap");
            View v = inflater.inflate(layout, container, false);
            v.setBackgroundResource(bitmap);    
            return v;
        }

        public static FirstFragment newInstance(int layout, int bitmap) {

            FirstFragment f = new FirstFragment();
            Bundle b = new Bundle();
            b.putInt("layout", layout);
            b.putInt("bitmap",bitmap);
            f.setArguments(b);
            return f;
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!