我制作了一个幻灯片横幅,在 Android 中使用 ViewPager 显示 3 张图片,但现在我需要它在 3 张图片之间每 5 秒更改一次。
我尝试了一个线程,但是当我达到 5 秒时,我不知道如何进行更改。
任何人都可以帮助我吗?
这是我的主要
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
viewPager = (ViewPager) findViewById(R.id.viewPagerMain);
adapter = new ViewPagerAdapter(MainActivity.this,imagesArray);
viewPager.setAdapter(adapter);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView tvEventos = (TextView) findViewById(R.id.eventos);
tvEventos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, EventActivity.class);
startActivity(i);
}
});
TextView tvPromociones = (TextView) findViewById(R.id.promociones);
tvPromociones.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, EventActivity.class);
startActivity(i);
}
});
TextView tvDirectory = (TextView) findViewById(R.id.directorio);
tvDirectory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, DirectoryActivity.class);
startActivity(i);
}
});
TextView tvPinatas = (TextView) findViewById(R.id.pinatas);
tvPinatas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, CumpleaniosActivity.class);
startActivity(i);
}
});
TextView tvBebes = (TextView) findViewById(R.id.bebes);
tvBebes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, BebesActivity.class);
startActivity(i);
}
});
}
这是我的 ViewPagerAdapter
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
viewPager = (ViewPager) findViewById(R.id.viewPagerMain);
adapter = new ViewPagerAdapter(MainActivity.this,imagesArray);
viewPager.setAdapter(adapter);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView tvEventos = (TextView) findViewById(R.id.eventos);
tvEventos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, EventActivity.class);
startActivity(i);
}
});
TextView tvPromociones = (TextView) findViewById(R.id.promociones);
tvPromociones.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, EventActivity.class);
startActivity(i);
}
});
TextView tvDirectory = (TextView) findViewById(R.id.directorio);
tvDirectory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, DirectoryActivity.class);
startActivity(i);
}
});
TextView tvPinatas = (TextView) findViewById(R.id.pinatas);
tvPinatas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, CumpleaniosActivity.class);
startActivity(i);
}
});
TextView tvBebes = (TextView) findViewById(R.id.bebes);
tvBebes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, BebesActivity.class);
startActivity(i);
}
});
}
我不知道在哪里创建线程以及如何更改位置。