SoFunction
Updated on 2025-04-08

ViewPager+PagerAdapter implements a boot page with indicator

A simple boot page consists of ViewPager and PagerAdapter for your reference. The specific content is as follows

package ;

import ;
import ;

import ;
import ;
import .;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
  *
  * @ClassName
  * @Description TODO Boot Page
  * @author Smile
  * @version v1.0
  * @date September 26, 2016
  */
public class MainActivity extends Activity {

  protected static final String TAG = "MainActivity";
  ViewPager mViewPager;
  List<ImageView> list;
  private LinearLayout mLinearLayout;

  // The distance between two small gray dots  private int poitWidth;
  private ImageView mViewPress;
  private ImageView mViewNotPress;
  private Button btnStart;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    initViews();
    initDatas();
  }

  private void initViews() {
    mViewPager = (ViewPager) findViewById(.main_view_pager);
    mLinearLayout = (LinearLayout) findViewById(.main_point);
    mViewPress = (ImageView) findViewById(.main_red_point_press);
    btnStart = (Button) findViewById(.main_btn);
  }

  private void initDatas() {
    list = new ArrayList<ImageView>();
    final int imageId[] = { , ,  };
    for (int i = 0; i < ; i++) {
      ImageView imageView = new ImageView(this);
      (imageId[i]);
      (imageView);
      mViewNotPress = new ImageView(this);
      (.shape_grey_point_not_press);
      LayoutParams layoutParams = new (.WRAP_CONTENT,
          .WRAP_CONTENT);
      if (i != 0) {
         = 20;
      }
      (mViewNotPress, layoutParams);

    }

    // Since the interface has not been drawn yet when executing onCreate(), all the poitWidth cannot be obtained.    // You must get the view tree and complete the drawing of the monitor view to get the poitWidth    ().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

      @Override
      public void onGlobalLayout() {
        poitWidth = (1).getLeft() - (0).getLeft();
        (TAG, poitWidth + "");
      }
    });
    (new ViewPgerAdapter(this, list));

    (new OnPageChangeListener() {

      // Which interface is currently selected      @Override
      public void onPageSelected(int position) {
        if (position==-1) {
          ();
        }
        else {
          ();
        }
      }

      // When the interface slides, this method will be called back continuously      // The second parameter is the percentage of the current page sliding (0.0 to 1.0)      @Override
      public void onPageScrolled(int position, float offset, int arg2) {
        (TAG, "arg1:" + offset);
        // The distance at which the small red dot currently slides        int width = (int) (poitWidth * offset + position * poitWidth);
         layoutParams = () ();
         = width;
        (layoutParams);
      }

      @Override
      public void onPageScrollStateChanged(int arg0) {

      }
    });
  }

}
package ;

import ;

import ;
import .;
import ;
import ;
import ;

public class ViewPgerAdapter extends PagerAdapter {

  Context mContext;
  List<ImageView> list;

  public ViewPgerAdapter(Context context, List<ImageView> list) {
     = context;
     = list;
  }

  // Number of pages  @Override
  public int getCount() {
    return ();
  }

  // Multiplex page card  @Override
  public boolean isViewFromObject(View view, Object obj) {
    return view == obj;
  }

  // Destroy the page card  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    ((position));
  }

  // Generate page card  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    View view = (position);
    (view);
    return view;
  }

}

shape file

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro
  android:shape="oval" >

  <size
    android:height="10dp"
    android:width="10dp" />
  <solid android:color="#C3C3C3"/>

</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro
  android:shape="oval" >

  <size
    android:height="10dp"
    android:width="10dp" />
  <solid android:color="#44FF0000"/>

</shape>
&lt;RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="" &gt;

  &lt;.
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent" &gt;
  &lt;/.&gt;

  &lt;RelativeLayout
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="50dp" &gt;

    &lt;LinearLayout
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal" &gt;
    &lt;/LinearLayout&gt;

    &lt;ImageView
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/shape_red_point_press" /&gt;
  &lt;/RelativeLayout&gt;

  &lt;Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    android:visibility="gone"
    android:layout_above="@id/main_relative"
    android:layout_centerHorizontal="true"
    android:text="Start Experience" /&gt;

&lt;/RelativeLayout&gt;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.