选项卡布局中的ImageView数据在页面切换后消失。

huangapple go评论100阅读模式
英文:

Tablayout Imageview data disappeared after page change

问题

Tablayout在页面切换后ImageView的数据消失了,每个选项卡中都有两个ImageView,从图库选择的图片在切换选项卡时消失了,有什么办法可以解决这个问题。

我还尝试了:
https://stackoverflow.com/questions/27142850/tab-content-disappeared-after-change-page

PagerAdapter

public class PagerAdapter extends FragmentStatePagerAdapter {
    final int pageCount = 3;
    private String tabTitles[] = new String[]{"Tab 1", "Tab 2", "Tab 3"};

    public PagerAdapter(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FirstFragment();
            case 1:
                return new SecondFragment();
            case 2:
                return new ThirdFragment();
            default:
                return null;
        }

    }

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

    @Override
    public int getCount() {
        return pageCount;
    }
}

FirstFragment

public class FirstFragment extends Fragment implements View.OnClickListener {
    ImageView imageButton1;
    ImageButton imageButton2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_first, container, false);
        imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
        imageButton2 = (ImageButton) v.findViewById(R.id.secondimagebtn);
        imageButton1.setOnClickListener(this::onClick);
        imageButton2.setOnClickListener(this::onClick);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.firstimagebtn:
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
                break;
            case R.id.secondimagebtn:
                Intent intent2 = new Intent(Intent.ACTION_PICK);
                intent2.setType("image/*");
                startActivityForResult(intent2, 1);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = data.getData();
                    imageButton1.setImageURI(selectedImage);
                }
                break;
            case 1:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = data.getData();
                    imageButton2.setImageURI(selectedImage);
                }
                break;
        }
    }
}
英文:

Tablayout Imageview data disappeared after page change , i have two imageview in each tab , the images is selected from the gallery is dissapared when i change tab, any idea how can i fix this

i also tried
https://stackoverflow.com/questions/27142850/tab-content-disappeared-after-change-page

PagerAdapter

public class PagerAdapter extends FragmentStatePagerAdapter
{
final int pageCount = 3;
private String tabTitles[] = new String[]{"Tab 1","Tab 2","Tab 3",};
public PagerAdapter(@NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
case 2:
return new ThirdFragment();
default:
return null;
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
@Override
public int getCount() {
return pageCount;
}
}

FirstFragment

public class FirstFragment extends Fragment implements View.OnClickListener{
ImageView imageButton1;
ImageButton imageButton2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
imageButton2 = (ImageButton)v.findViewById(R.id.secondimagebtn);
imageButton1.setOnClickListener(this::onClick);
imageButton2.setOnClickListener(this::onClick);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.firstimagebtn:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,0);
break;
case R.id.secondimagebtn:
Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setType("image/*");
startActivityForResult(intent2,1);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == Activity.RESULT_OK){
Uri selectedImage = data.getData();
imageButton1.setImageURI(selectedImage);
}
break;
case 1:
if(resultCode == Activity.RESULT_OK){
Uri selectedImage = data.getData();
imageButton2.setImageURI(selectedImage);
}
break;
}
}

答案1

得分: 0

由于您使用了 FragmentStatePagerAdapter,请牢记以下内容:

当页面数量较多时,这个版本的分页器更加有用,更像是一个列表视图。当页面对用户不可见时,它们的整个片段可能会被销毁,只保留该片段的保存状态。与 FragmentPagerAdapter 相比,这使得分页器在每个访问过的页面关联的内存方面可以保留更少的内存,但在页面之间切换时可能会产生更多的开销。

简而言之,您的 FirstFragment 中的 ImageView 数据消失是因为在切换到另一个选项卡/片段时,FirstFragment 被销毁,这是 FragmentStatePagerAdapter 的特性。

现在,您可以通过适当地保存片段的状态来解决这个问题:

public class FirstFragment extends Fragment implements View.OnClickListener {
    ImageView imageButton1;
    ImageButton imageButton2;

    private static final String ARG_URI_IMAGE_1 = "image1Uri";
    private static final String ARG_URI_IMAGE_2 = "image2Uri";

    private String mUriImg1;
    private String mUriImg2;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mUriImg1 = savedInstanceState.getString(ARG_URI_IMAGE_1);
            mUriImg2 = savedInstanceState.getString(ARG_URI_IMAGE_2);
            if (mUriImg1 != null) {
                imageButton1.setImageURI(Uri.parse(mUriImg1));
            }
            if (mUriImg2 != null) {
                imageButton2.setImageURI(Uri.parse(mUriImg2));
            }
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        if (mUriImg1 != null) {
            outState.putString(ARG_URI_IMAGE_1, mUriImg1);
        }
        if (mUriImg2 != null) {
            outState.putString(ARG_URI_IMAGE_2, mUriImg2);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (resultCode == Activity.RESULT_OK) {
                    Uri uri = data.getData();
                    mUriImg1 = uri.toString();
                    imageButton1.setImageURI(uri);
                }
                break;
            case 1:
                if (resultCode == Activity.RESULT_OK) {
                    Uri uri = data.getData();
                    mUriImg2 = uri.toString();
                    imageButton2.setImageURI(uri);
                }
                break;
        }
    }
}
英文:

Since you use FragmentStatePagerAdapter, please keep in mind that:

> This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

In short, the ImageView's data on your FirstFragment disappeared because FirstFragment was destroyed when you switched to another tab/fragment as the nature of FragmentStatePagerAdapter

Now you can solve this problem simply by saving state for fragment properly

public class FirstFragment extends Fragment implements View.OnClickListener{
ImageView imageButton1;
ImageButton imageButton2;
private static final String ARG_URI_IMAGE_1 = "image1Uri"
private static final String ARG_URI_IMAGE_2 = "image2Uri"
private String mUriImg1;
private String mUriImg2; 
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
mUriImg1 = savedInstanceState.getString(ARG_URI_IMAGE_1);
mUriImg2 = savedInstanceState.getString(ARG_URI_IMAGE_2);
if (mUriImg1 != null) {
imageButton1.setImageURI(Uri.parse(mUriImg1))
}
if (mUriImg2 != null) {
imageButton2.setImageURI(Uri.parse(mUriImg2))
}
}
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
if (mUriImg1 != null) {
outState.putString(ARG_URI_IMAGE_1, mUriImg1);
}
if (mUriImg2 != null) {
outState.putString(ARG_URI_IMAGE_2, mUriImg2);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == Activity.RESULT_OK){
Uri uri = data.getData();
mUriImg1 = uri.toString();
imageButton1.setImageURI(uri);
}
break;
case 1:
if(resultCode == Activity.RESULT_OK){                  
Uri uri = data.getData();
mUriImg2 = uri.toString();
imageButton2.setImageURI(uri);
}
break;
}
}
}

huangapple
  • 本文由 发表于 2020年10月20日 07:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64436362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定