2017-07-26 41 views
0

我想在選項卡滑塊的選項卡內製作圖像滑塊。任何人都可以引導我嗎?在Pagerslidingtabstrip中使用ViewPager的圖像滑塊

這是用於圖像滑塊的CustomSliderAdapter.java。

public class CustomSlideAdapter extends FragmentPagerAdapter { 

    private int [] image_resources={R.drawable.home,R.drawable.create,R.drawable.map}; 
    private Context ctx; 
    private LayoutInflater layoutInflater; 

    public CustomSlideAdapter(Context ctx, FragmentManager fm){ 
     super(fm); 
     this.ctx=ctx; 
    } 
    @Override 
    public int getCount() { 
     return image_resources.length; 
    } 

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

    @Override 
    public Fragment getItem(int position) { 

     return null; 
    } 

    /*@Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     layoutInflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view_img=layoutInflater.inflate(R.layout.slider_layout,container,false); 
     ImageView imageView=(ImageView) view_img.findViewById(R.id.image_slide_view); 
     /* TextView imageSlideText=(TextView) view_img.findViewById(R.id.image_slider_text); 
     imageView.setImageResource(image_resources[position]); 
     container.addView(view_img); 
     return view_img; 
    }*/ 

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

這是要添加到我的選項卡適配器中的一個片段。

public class Image_slider extends Fragment { 

    ViewPager viewPager; 
    CustomSlideAdapter adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
     View result=inflater.inflate(R.layout.slider_layout, container, false); 
     ViewPager pager=(ViewPager)result.findViewById(R.id.image_slider); 

     return(result); 
    } 
    private PagerAdapter buildAdapter() { 
     return(new CustomSlideAdapter(getActivity(),getChildFragmentManager())); 
    } 

} 

請你告訴我,我怎樣才能實現這個正確的解決方案..

回答

0

你可以嘗試這樣的,

CustomSlideAdapter.java

public class CustomSlideAdapter extends PagerAdapter { 

    private int [] image_resources={R.drawable.home,R.drawable.create,R.drawable.map}; 

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

    @Override 
    public View instantiateItem(ViewGroup container, int position) { 
     ImageView photoView = new ImageView(container.getContext()); 

     photoView.setImageResource(image_resources[position]); 

     container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 

     return photoView; 
    } 

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

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

} 

Image_slider.java

public class Image_slider extends Fragment { 

    ViewPager viewPager; 
    CustomSlideAdapter adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
     View result=inflater.inflate(R.layout.slider_layout, container, false); 
     ViewPager pager=(ViewPager)result.findViewById(R.id.image_slider); 

     adapter = new CustomSlideAdapter(); 

     pager.setAdapter(adapter); 

     return(result); 
    } 

} 
+0

它沒有工作我的應用程序崩潰。 –