Search code examples
androidandroid-fragmentsandroid-viewpagerbundle

How to change child fragment imageView from ViewPager


I have ViewPager and 4 fragments in it. Fragment 2 called StatusFragment. In that StatusFragment I have an ImageView. In ViewPager I'm getting bundle of String, that's an image path. I'm getting my bundle successfully, but now i need to decode my path with bitmap and add it in ImageView of StatusFragment. I tried to do ImageView from StatusFragment public, and change it from ViewPager, but that gives NullPointerException. How can I change that Image from ViewPager? Thank you.

Here the ViewPager code.

public class SlidePageTabsMainFragment extends BaseFragment {
private View mainView;
private TabLayout mTabs;
private ViewPager mPage;
private FragmentActivity myContext;
private CoordinatorLayout coordinatorLayout;
private StatusFragment statusFragment;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.slide_page_tabs_main_fragment, container, false);
    mTabs = (TabLayout) mainView.findViewById(R.id.tab_layout);
    mPage = (ViewPager) mainView.findViewById(R.id.view_pager);
    coordinatorLayout = (CoordinatorLayout) mainView.findViewById(R.id.coordinator_layout_id);

    mPage.setOffscreenPageLimit(3);

    //mTabs.setupWithViewPager(mPage);
    SetUpViewPager();
    getTabLayoutLocation();
    setmFloatingActionButtons();

    Bundle getBundleImagePath = getArguments();
    if(getBundleImagePath != null){
        String getImagePaths = getBundleImagePath.getString("imagePath");
        Toast.makeText(myContext, getImagePaths, Toast.LENGTH_SHORT).show();

        Bitmap imageBitmap = BitmapFactory.decodeFile(getImagePaths);
        //statusFragment.photoId.setImageBitmap(imageBitmap);

        mPage.setCurrentItem(1);
    }
    return mainView;
}
}

@Override
public String toString() {
    return "SlidePageTabsMainFragment";
}

public void SetUpViewPager() {
    //Add fragments
    SlidePageTabsAdapter adapter = new SlidePageTabsAdapter(getChildFragmentManager());
    adapter.addFragment(new SupportopFragment());
    adapter.addFragment(new StatusFragment());
    adapter.addFragment(new ConnectionsFragment());
    adapter.addFragment(new SupportersFragment());

    //Setting adapter
    mPage.setAdapter(adapter);
    mTabs.setupWithViewPager(mPage);

    //Setting icons in tabs
    mTabs.getTabAt(0).setIcon(R.drawable.supportop_icon);
    mTabs.getTabAt(1).setIcon(R.drawable.repair_icon);
    mTabs.getTabAt(2).setIcon(R.drawable.transport_icon);
    mTabs.getTabAt(3).setIcon(R.drawable.security_icon);
    //mPage.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabs));

    mPage.addOnPageChangeListener(pageChangeListener);

    // mTabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mPage));

}
}

Ok that was a little big code, but I think you understood it.

Now i'll add the StatusFragment.

public class StatusFragment extends BaseFragment {

private View mainView;
public ImageView photoId;
private SlidePageTabsMainFragment slidePages;

public StatusFragment() {};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mainView = inflater.inflate(R.layout.status_fragment, container, false);

    photoId = (ImageView) mainView.findViewById(R.id.photo_id);
    slidePages = new SlidePageTabsMainFragment();

    return mainView;
}

@Override
public String toString() {
    return "StatusFragment";
}
}

Ok, now when you watched the code, so how can I change photoId image from ViewPager, if I'm getting the bundle in ViewPager? Don't say to get the bundle in StatusFragment. I need to get it in ViewPager only. Thank you.


Solution

  • Just set a bundle to your StatusFragment with the Bitmap into it. Bitmap implements Parcelable so it can be stored in a Bundle:

    In SlidePageTabsMainFragment

    public void setUpViewPager(Bitmap b) {
        Bundle args = new Bundle();
        args.putParcelable("image", b);
        //... your code
        // Add arguments to the Fragment
        Fragment statusFragment = new StatusFragment();
        statusFragment.setArguments(args);
    
        //...   
        adapter.addFragment(statusFragment);
        //...
    }
    

    In StatusFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        mainView = inflater.inflate(R.layout.status_fragment, container, false);
    
        photoId = (ImageView) mainView.findViewById(R.id.photo_id);
    
        // Get the Bitmap and set it to the ImageView
        Bundle bundle = getArguments();
        Bitmap image = (Bitmap) bundle.getParcelable("image");
        photoId.setImageBitmap(image);
    
        slidePages = new SlidePageTabsMainFragment();
    
        return mainView;
    }