3

이에 대한 조언이 필요합니다. ViewPager를 사용하여 조각이 있습니다. 나는 몇 개의 이미지가 담긴 갤러리로 사용한다. 이미지는 웹에서로드되고 비트 맵 배열에 저장됩니다. instatiateItem 및 다른 방법은 현재 사용되지 않습니다조각 관리, public static viewpager

우선 사용에서

..

public class GalleryPageAdapter extends PagerAdapter { 

public GalleryPageAdapter(){ 

} 
    public Object instantiateItem(View collection, int position) { 
    } 
... 
//all other methods 
} 

하지만 ... 나는 몇 가지 조사를 만든 작동 이제 다른 게시물을 Class extending Fragment - error says make sure class name exists, is public, and has an empty constructor that is publicUnable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

을 따라 오류가없는

public class sEstablecimiento extends android.support.v4.app.FragmentActivity{ 
static BitmapDrawable iconos[]; 
//load bitmaps into iconos[] from web 

static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 
    static int position; 
    public ScreenSlidePagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     this.position=position; 
     return new ScreenSlidePageFragment(); 
    } 

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

} 

public static class ScreenSlidePageFragment extends Fragment { 
    private BitmapDrawable image; 

    public ScreenSlidePageFragment() { 
     image=iconos[ScreenSlidePagerAdapter.position]; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
          Bundle savedInstanceState) { 
     ViewGroup rootView =(ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container, false); 
     ImageView icono=(ImageView)rootView.findViewById(R.id.imagen); 
     icono.setImageDrawable(iconos[ScreenSlidePagerAdapter.position]); 
     return rootView; 
    } 

    } 

언제나처럼 여기 클래스와 조각의 방법으로

  1. 내 특정 질문은 정적, 그것은 정적하는 BitmapDrawable 배열을 필요로하고 몇 images.It이 때 BitmapDrawable 배열 '널 (null)'할 괜찮 보유하고 있습니다 활동은 기억을 풀기 위해 파괴된다? (조각을 리필하고 다른 활동에서 사용)
  2. 마지막 코드는 정적 클래스 또는 정적 배열이 필요하지 않습니다. 그것은 권장되지 않는대로 코드를 유지하는 것이 좋습니다.
  3. 무엇이 비추천 버전의 코드를 유지한다는 의미입니까?

귀하의 시간과 출석보다.

답변

1

이 사용 사례의 원래 코드로 되돌아 가야한다고 생각합니다. 단지 이미지 갤러리를 표시하고 싶다면 Fragments가 아닌 Views를 기반으로 ViewPager를 만드는 것이 훨씬 간단합니다. 각 페이지에 대해 더 복잡한 화면이있는 경우 일반적으로 단편을 사용합니다. ViewPager가 최상위 탐색 UI 인 경우

정적 인 Drawable 참조로 실행할 수있는 문제에 대해서는 꽤 오래된 것이지만 still relevant article입니다. Drawables에는 Activity에 대한 참조가있는 Views에 대한 참조가 있습니다. 정적 Drawable 참조를 사용하여 Activity 컨텍스트를 쉽게 누출시킬 수 있습니다. 세로 및 세로 방향 모두를 지원하면 장치를 돌릴 때마다 작업이 파괴되고 다시 생성됩니다. 이 경우로드 된 비트 맵을 유지하는 방법에주의해야합니다 (회전 할 때가 아니라 사용자가 액티비티를 떠날 때만 배열을 지우려는 경우).

  1. public Object instantiateItem (View container, int position)
  2. public Object instantiateItem (ViewGroup container, int position)

원본 (# 1) 번째 방법 대신 중단된다

는 PagerAdapter 클래스는 실제로 instantiateItem 방법의 두 가지 버전이있다. instantiateItem을 오버라이드 (override) 할 때 일반적으로 뷰를 컨테이너에 추가해야합니다. 첫 번째 메소드 서명을 사용하려면 추가하기 전에 먼저 컨테이너를 ViewGroup에 캐스팅해야합니다. 두 번째 메소드 서명은 처음부터 ViewGroup을 제공함으로써이를 방지합니다. PagerAdapter에는 비슷하게 사용되지 않는 여러 가지 메소드가 있으며, 모두보기와 달리 ViewGroup을 사용하는 동일한 버전이 있습니다.

public class GalleryPagerAdapter extends PagerAdapter { 

    private Context mContext; 
    private BitmapDrawable[] mIcons; 

    public GalleryPagerAdapter(Context context, BitmapDrawable[] icons) { 
     mContext = context; 
     mIcons = icons; 
    } 

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

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == object; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, final int position) { 
     // You can inflate a layout here instead and apply styling to the ImageView 
     ImageView imageView = new ImageView(mContext); 

     BitmapDrawable icon = mIcons[position]; 
     imageView.setImageDrawable(icon); 

     container.addView(imageView, 0); 
     return imageView; 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((View) object); 
    } 
} 
+0

내 원래의 코드는 당신이 게시 한 그대로입니다 : 여기

이미지를 표시하는 PagerAdapter를 구현하는 몇 가지 (검증되지 않은) 샘플 코드입니다. 기사 주셔서 감사합니다. 이 경우 이전 코드를 유지하고 일부 실험에서는 기본 탐색 메뉴에서 새로운 방법을 사용합니다. 감사합니다. – UrielUVD