2016-09-27 5 views
1

これは私のPagerAdapterです。フラグメント内のPagerAdapterが最初のビューのみを表示するのはなぜですか?

public class SlideAdapter extends PagerAdapter{ 
    private List<ImageView> views; 

    public SlideAdapter (List<ImageView> views){ 
     this.views=views; 
    } 

    @Override 
    public void destroyItem(View container, int position, Object object) { 
     ((ViewPager) container).removeView(views.get(position)); 
    } 

    @Override 
    public int getCount() { 
     return views.size(); 
    } 

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

    @Override 
    public Object instantiateItem(View container, int position) { 
     ((ViewPager) container).addView(views.get(position)); 
     return views.get(position); 
    } 

} 

これは私の断片です。

public class sportfragment extends Fragment { 

    private ViewPager slideviewpager; 
    private Boolean isContinue=true; 
    private AtomicInteger what=new AtomicInteger (0); 
    private ImageView[] indiimageview; 
    private List<ImageView> pics; 

    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 


    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    public sportfragment() { 

    } 


    public static sportfragment newInstance(String param1, String param2) { 
     sportfragment fragment = new sportfragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

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

     View view=inflater.inflate(R.layout.fragment_sportfragment, container, false); 

     slideviewpager=(ViewPager) view.findViewById(R.id.slideviewpager); 
     ViewGroup indicatorgroup=(ViewGroup) view.findViewById(R.id.indicatorgroup); 

     pics=new ArrayList<ImageView>(); 

     ImageView slide1=new ImageView(view.getContext()); 
     slide1.setBackgroundResource(R.drawable.slide1); 
     pics.add(slide1); 

     ImageView slide2=new ImageView(view.getContext()); 
     slide1.setBackgroundResource(R.drawable.slide2); 
     pics.add(slide2); 

     ImageView slide3=new ImageView(view.getContext()); 
     slide1.setBackgroundResource(R.drawable.slide3); 
     pics.add(slide3); 

     ImageView slide4=new ImageView(view.getContext()); 
     slide1.setBackgroundResource(R.drawable.slide4); 
     pics.add(slide4); 

     indiimageview=new ImageView[pics.size()]; 

     for (int i=0;i<pics.size();i++) { 
      ImageView buff=new ImageView(view.getContext()); 
      buff.setLayoutParams(new LinearLayout.LayoutParams(20,20)); 
      buff.setPadding(5,5,5,5); 
      indiimageview[i]=buff; 
      if (i==0) { 
       indiimageview[i].setBackgroundResource(R.drawable.focus); 
      }else { 
       indiimageview[i].setBackgroundResource(R.drawable.blur); 
      } 
      indicatorgroup.addView(indiimageview[i]); 
     } 

     slideviewpager.setAdapter(new SlideAdapter(pics)); 
     slideviewpager.setOnPageChangeListener(new GuidePageChangeListener()); 

     return view; 
    } 


    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 


    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 

    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 


    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 

    private final class GuidePageChangeListener implements ViewPager.OnPageChangeListener{ 

     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 

      for (int i=0;i<indiimageview.length;i++) { 

       if (i==position) { 
       //4 points change with viewpager 
        indiimageview[i].setBackgroundResource(R.drawable.focus); 
       } else { 
        indiimageview[i].setBackgroundResource(R.drawable.blur); 
       } 
      } 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    } 
} 

最初のページはイメージです。 他の3は空白です。 これを解決する方法はありますか?

答えて

0

ViewPagesは、ビューではなくフラグメントで使用するためのものです。つまり、単純な古いビューではなく、ImageViewsをフラグメントとしてパッケージ化する必要があります。代わりにImageViewsのリストで、あなたが持っているだろう、

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:adjustViewBounds="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"/> 

</RelativeLayout> 

MyImageView.java

public class MyImageFragment extends Fragment { 

    private ImageView mImageView; 
    private int resourceId; 

    public static MyImageFragment newInstance(int resourceId) { 
     MyImageFragment fragment = new MyImageFragment(); 

     Bundle args = new Bundle(); 
     args.putInt("resource_id", resourceId); 
     fragment.setArguments(args); 

     return fragment; 
    } 

    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final Bundle args = getArguments(); 
     if (args != null && !loadedFromArgs) { 
      url = (String)args.getSerializable("url"); 
      resourceId = args.getInt("in_list", -1); 
      loadedFromArgs = true; 
     } 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     ViewGroup rootView = (ViewGroup) inflater.inflate(
       R.layout.fragment_my_image, container, false); 

     mImageView = (ImageView) rootView.findViewById(R.id.image); 
     mImageView.setBackgroundDrawable(getResources().getDrawable(resourceId)) 
     return rootView; 
    } 
} 

fragment_my_image.xml:

は、次の例を考えてみましょうPagerAdapterが指すMyImageFragmentのリスト。

関連する問題