Multiple pages at the same time on a ViewPager

前端 未结 8 1445
执念已碎
执念已碎 2020-12-07 09:58

Is there a possibility to display two pages at the same time, when using a ViewPager? I\'m not looking for an edge effect, but rather for two full

8条回答
  •  星月不相逢
    2020-12-07 10:47

    It can be don in the Item Layout of the Viewpager. Assume that, you want two pages at a time.

    This is the Item Layout (photo_slider_item.xml):

        
            
            
        
    

    And in your PagerAdapter:

    @Override
    public View instantiateItem(ViewGroup container, int position){
        View sliderItem = LayoutInflater.from(container.getContext()).inflate(photo_slider_item, container, false);
        ImageView photoBox1 = (ImageView) sliderItem.findViewById(R.id.photoBox1);
        ImageView photoBox2 = (ImageView) sliderItem.findViewById(R.id.photoBox2);
        photoBox1.setImageResource(photosIds[position]);
        if(position < photosIds.length-1){
            photoBox2.setImageResource(photosIds[position+1]);
        } else {photoBox2.setImageResource(photosIds[0]);}
        container.addView(sliderItem);
        return sliderItem;
    }
    

    Edited version for more than two items

    if you want more than two items, first add your items to the LinearLayout then use following algorithm:

    photoBox1.setImageResource(photosIds[position]);
    if(position < photosIds.length-i-1){
       photoBox2.setImageResource(photosIds[position+1]);
       photoBox3.setImageResource(photosIds[position+2]);
       .
       .
       .
       photoBox(i).setImageResource(photosIds[position+i-1]);
    } else if(position < photosIds.length-i-2){
       photoBox2.setImageResource(photosIds[position+1]);
       photoBox3.setImageResource(photosIds[position+2]);
       .
       .
       .
       photoBox(i-1).setImageResource(photosIds[position+i-2]);
       photoBox(i).setImageResource(photosIds[0]);
    } . . . else if(position < photosIds.length-1){
       photoBox2.setImageResource(photosIds[position+1]);
       photoBox3.setImageResource(photosIds[0]);
       photoBox4.setImageResource(photosIds[1]);
       .
       .
       .
       photoBox(i-1).setImageResource(photosIds[i-2-2]);
       photoBox(i).setImageResource(photosIds[i-2-1]);
    } else {
        photoBox2.setImageResource(photosIds[0]);
        photoBox3.setImageResource(photosIds[1]);
        .
        .
        .
        photoBox(i-1).setImageResource(photosIds[i-1-2]);
        photoBox(i).setImageResource(photosIds[i-1-1]);
    }
    

    i : number of your items

    [i-2-2] : number 2 in the middle is number of items in the last page of the viewpager.

提交回复
热议问题