如何从ViewPager更改子片段imageView

时间:2018-03-13 10:30:34

标签: android android-fragments android-viewpager bundle

我有ViewPager和4个片段。片段2称为StatusFragment。在那StatusFragment我有一个ImageView。在ViewPager我获得了bundle字符串,这是一个图像路径。我已成功获取我的捆绑包,但现在我需要使用位图解码我的路径并将其添加到ImageView的{​​{1}}中。我尝试从StatusFragment公开ImageView进行StatusFragment,并从ViewPager更改,但这会NullPointerException。如何从ViewPager更改该图像?谢谢。

这里是ViewPager代码。

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));

}
}

好的,这是一个很大的代码,但我认为你理解它。

现在我将添加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";
}
}

好的,现在当你看到代码时,如果我在photoId收到包裹,我怎样才能从ViewPager更改ViewPager图片?不要说要在StatusFragment中获取捆绑包。我只需要在ViewPager中获取它。谢谢。

1 个答案:

答案 0 :(得分:1)

只需将Bitmap设置为StatusFragment即可。 Bitmap实现了Parcelable,因此它可以存储在Bundle:

在SlidePageTabsMainFragment中

@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;
}

在StatusFragment中

HRESULT OpenFromInitializationString(
                _In_z_ LPCOLESTR szInitializationString,
                _In_ bool fPromptForInfo = false) throw()
{
  CComPtr<IDataInitialize> spDataInit;

  HRESULT hr = CoCreateInstance(__uuidof(MSDAINITIALIZE), NULL, CLSCTX_INPROC_SERVER, __uuidof(IDataInitialize), (void**)&spDataInit);

  if (FAILED(hr))
    return hr;

  hr = spDataInit->GetDataSource(NULL, CLSCTX_INPROC_SERVER, szInitializationString, __uuidof(IDBInitialize), (IUnknown**)&m_spInit);

  if (FAILED(hr))
    return hr;