A Useful Open Source Framework for RollView Pager

Keywords: github Android

Github address: https://github.com/Jude95/RollViewPager
Supports infinite loops. Play is paused when touching, and continues after a delay period of touching is over. It looks like this. Indicators can be customized for points, numbers, and positions.

rely on

compile 'com.jude:rollviewpager:1.4.6'

Use

<com.jude.rollviewpager.RollPagerView
    android:layout_width="match_parent"
    android:layout_height="180dp"
    app:rollviewpager_play_delay="3000"/>

app:rollviewpager_play_delay= "3000" playback interval, unit ms. Fill in 0 and don't play. The default is 0 app:rollviewpager_hint_gravity= "center" indicator location, providing left,center,right. Default Center
app:rollviewpager_hint_color=" 7c7c7c" indicator background color. Default black
app:rollviewpager_hint_alpha= "80" indicator background transparency. 0 transparent, 255 opaque. Default 0.
app:rollviewpager_hint_paddingLeft= "16dp" indicator left margin
app:rollviewpager_hint_paddingRight= "16dp" indicator right margin
app:rollviewpager_hint_paddingTop= "16dp" indicator upper margin
app:rollviewpager_hint_paddingBottom= "16dp" indicator lower margin
It's good to specify the interval.

HintView

HintView is provided to customize the indicator.

setHintView(HintView hintview)
  • HintView
  • ShapeHintView
    -IconHintView
    -ColorPointHintView
  • TextHintView
    Usage:
mRollViewPager.setHintView(new IconHintView(this,R.drawable.point_focus,R.drawable.point_normal));
mRollViewPager.setHintView(new ColorPointHintView(this, Color.YELLOW,Color.WHITE));
mRollViewPager.setHintView(new TextHintView(this));
mRollViewPager.setHintView(null);//Hidden indicator

ItemClickListener

mRollViewPager.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(int position) {
      Toast.makeText(MainActivity.this,"Item "+position+" clicked",Toast.LENGTH_SHORT).show();
  }
});

Adapter

Pager Adapter provides three conveniences for use.
This ViewPager can also use any other Pager Adapter.

StaticPagerAdapter

Adadapter for storing pages. When view is added, it will be stored without getView again, which reduces the consumption of page creation and memory consumption. Generally, this scheme is better in the case of automatic broadcasting. Otherwise, View will be constructed a lot. The concept refers to FragmentPager Adapter. It can be used for other ViewPager s.

DynamicPagerAdapter

Dynamic Adapter. When the 3rd view is created, the 1st view is destroyed (recursive) and getView is often called. Increase page creation consumption and reduce memory consumption. The concept refers to FragmentState Pager Adapter. It can be used for other ViewPager s.

//Two adapters are used the same way.
mRollViewPager.setAdapter(new TestNomalAdapter());
private class TestNomalAdapter extends StaticPagerAdapter{
    private int[] imgs = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
    };

    @Override
    public View getView(ViewGroup container, int position) {
        ImageView view = new ImageView(container.getContext());
        view.setImageResource(imgs[position]);
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return view;
    }

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

LoopPagerAdapter

An infinite loop Adapter. The infinite loop uses the getCount method to return the int large number (there is no shortcoming, in addition to estimating the interval of 1s you will not see him play in your lifetime). The experiment results are better than jumping from page N to page 1.
Static Pager Adapter is adopted for data. Save the cost of creating View. This Adapter can only be used for this RollViewPager;
No other settings are needed. It's simple.

mRollViewPager.setAdapter(new TestLoopAdapter(mRollViewPager));
private class TestLoopAdapter extends LoopPagerAdapter{
    private int[] imgs = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
    };

    public TestLoopAdapter(RollPagerView viewPager) {
        super(viewPager);
    }

    @Override
    public View getView(ViewGroup container, int position) {
        ImageView view = new ImageView(container.getContext());
        view.setImageResource(imgs[position]);
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return view;
    }

    @Override
    public int getRealCount() {
        return imgs.length;
    }
}

Playback control

rollViewPager.pause()
rollViewPager.resume()
rollViewPager.isPlaying()

Posted by artacus on Fri, 29 Mar 2019 16:48:28 -0700