在Pagerslidingtabstrip中使用ViewPager的图像滑块

时间:2017-07-26 20:05:18

标签: android android-viewpager fragmentpageradapter

我想在我的标签滑块的标签内制作图像滑块。有人可以指导我吗?

这是用于图像滑块的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()));
    }

}

请告诉我正确的解决方案我该如何实现这个...

1 个答案:

答案 0 :(得分: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);
    }

}