gpt4 book ai didi

android - fragment 选项卡 viewpager 问题

转载 作者:行者123 更新时间:2023-11-30 02:21:46 25 4
gpt4 key购买 nike

我有一个带有 View 寻呼机和三个标签的 fragment "FragmentOne""FragmentTwo"等

在每个 fragment (页面)中,我想设置来自图库或相机的图像。但是当我在 FragmentOne 选项卡中设置图像(位图)时,位图也显示在其他 fragment 中。但我希望独特的选定图像应显示在相应 fragment 的 Imageview 中。

我的 ViewPager 类看起来像。

public class MainFragment extends Fragment {
private ViewPager mViewPager;
private SlidingTabLayout mSlidingTabLayout;

// Tabs titles
private String[] tabsTitles = {"One", "Two", "Three"};




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

View rootView = inflater.inflate(R.layout.new_proj_frag_layout, container, false);
// ViewPager
mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), tabsTitles));

// Sliding tab layout
mSlidingTabLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
return rootView;
}

FragmentOne

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.white_fragment_layout, container, false);
image_one_btn = (ImageView) rootView.findViewById(R.id.image_one_click);
image_one_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});

return rootView;
}

private void selectImage() {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getParentFragment().startActivityForResult(i, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null);

cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Log.i("ImagePath:","===="+picturePath);

image_one_btn.setImageBitmap(BitmapFactory.decodeFile(picturePath));

cursor.close();
} else {
Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT).show();
}
}

ViewPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter {

private String[] tabsTitles;

public ViewPagerAdapter(FragmentManager fm, String[] tabsTitles) {
super(fm);
this.tabsTitles = tabsTitles;
}

@Override
public CharSequence getPageTitle(int position) {
return tabsTitles[position];
}

@Override
public Fragment getItem(int index) {
switch(index) {
case 0:
return new ImageOneFragment();
case 1:
return new ImageTwoFragment();
case 2:
return new ImageThreeFragment();

}

return null;
}

@Override
public int getCount() {
return tabsTitles.length;
}

最佳答案

这是可以帮助任何面临相同问题的人的解决方案。

MainFragment add the following code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

List<Fragment> fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);


Log.i("in activity result","");
}
}
}

并在 ImageOneFragment 的 onActivityResult 方法中检查 requestCode

if (requestCode == 1){

//将图片设置为imageview

关于android - fragment 选项卡 viewpager 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408927/

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