Songnae
507
2021-01-09 18:49:12
1
146

안드로이드 이미지 불러오기 질문있습니다


DB에 이미지를 uri의 텍스트로 저장하여 뷰페이저에 뿌려주는 프로그램을 작성하고 있습니다.

fragment3개를 만들어서 진행하는데 첫번째 페이지는 올바르게 이미지가 보이는데 2,3번째 부터는 그냥 하얗게 보이네요... fragment의 코드는 다 똑같은데 왜 그럴까요 ...


public class ContentMain extends AppCompatActivity implements Serializable {

    EditText title;
    EditText content;
    EditText date;
    ViewPager pager;
    ViewData item;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        Intent intent = getIntent();
        item = (ViewData)intent.getSerializableExtra("item");
        Log.d("DB", "" + item.getTitle() + item.getContent() + item.getDate() + item.getUri1()+ item.getUri2()+ item.getUri3()); //잘넘어옴

        setInit();
    }

    public void setInit() { //내용설정

        title = findViewById(R.id.content_title);
        date = findViewById(R.id.content_date);
        content = findViewById(R.id.content_content);


        title.setText(item.getTitle());
        date.setText(item.getDate());
        content.setText(item.getContent());

        setPager();


    }

    public void setPager() {//페이지 설정
        pager = findViewById(R.id.content_pager);
        pager.setOffscreenPageLimit(2);
        ContentPageAdapter adapter = new ContentPageAdapter(getSupportFragmentManager());
        ContentFragment1 fragment1 = new ContentFragment1(item.getUri1());
        ContentFragment2 fragment2 = new ContentFragment2(item.getUri2());
        ContentFragment3 fragment3 = new ContentFragment3(item.getUri3());
        adapter.addItem(fragment1);
        adapter.addItem(fragment2);
        adapter.addItem(fragment3);
        pager.setAdapter(adapter);


    }

    class ContentPageAdapter extends FragmentStatePagerAdapter{

        ArrayList<Fragment> items = new ArrayList<Fragment>();

        public ContentPageAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        public void addItem(Fragment item){
            items.add(item);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return items.get(position);
        }

        @Override
        public int getCount() {
            return items.size();
        }
    }
}
public class ContentFragment1 extends Fragment {
    ImageView imageView;
    String uri;

    public ContentFragment1(String uri) {
        this.uri = uri;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.content_fragment1, container, false);
        imageView = rootView.findViewById(R.id.content_fragment_image1);
        Log.d("DB", "content1 uri = " + uri);
        Glide.with(getContext()).load(uri).override(600,200).into(imageView);


        return rootView;
    }


}
public class ContentFragment2 extends Fragment {
    ImageView imageView;
    String uri;

    public ContentFragment2(String uri) {
        this.uri = uri;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.content_fragment2, container, false);
        imageView = rootView.findViewById(R.id.content_fragment_image2);
        Log.d("DB", "content2 uri = " + uri);
        //Glide.with(getContext()).load(uri).override(600,200).into(imageView);


        return rootView;
    }

 
}



0
  • 답변 1

  • 실크론
    358
    2021-01-09 20:19:17

    프레그먼트는 액티비티 빈곳에 뿌려주는거예요

    content_main.xml에  layout이나 fragment를 추가해준담에
    그 위치에 다른 fragment를 불러오는거예요
    getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, fragment4).commit();

    이런식으로 교체해주는거예요

  • 로그인을 하시면 답변을 등록할 수 있습니다.