gpt4 book ai didi

android - 如何从 ViewPager 更改子 fragment imageView

转载 作者:行者123 更新时间:2023-11-30 00:04:28 25 4
gpt4 key购买 nike

我有ViewPager里面有4个 fragment 。 fragment 2 称为 StatusFragment .在那StatusFragment我有一个 ImageView .在 ViewPager我得到 bundle字符串,这是一个图像路径。我成功地获得了我的包,但现在我需要用位图解码我的路径并将其添加到 ImageView 中的 StatusFragment .我试着做 ImageView来自 StatusFragment public,并将其从 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 中获得 bundle ?不要说要在 StatusFragment 中获取 bundle .我需要在 ViewPager 中获取它仅有的。谢谢。

最佳答案

只需将包含位图的 StatusFragment bundle 到其中。 Bitmap 实现了 Parcelable,因此它可以存储在 Bundle 中:

在 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);
//...
}

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

关于android - 如何从 ViewPager 更改子 fragment imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com