我有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
中获取它。谢谢。
答案 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;