如何在确定的时间内从 ViewPager 更改图像?
Posted
技术标签:
【中文标题】如何在确定的时间内从 ViewPager 更改图像?【英文标题】:How to change images from a ViewPager in a determined time? 【发布时间】:2017-10-31 21:02:27 【问题描述】:我制作了一个幻灯片横幅,在 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);
);
我不知道在哪里创建线程以及如何更改位置。
【问题讨论】:
嘿,添加一些源代码! 【参考方案1】:使用Handler#postDelayed(Runnable task, long delayInMillis)
:
int count = 0;
Handler handler = new Handler();
Runnable task = new Runnable()
@Override
public void run()
if (count == 3) return;
pager.setCurrentItem(count);
count++;
handler.postDelayed(this, 5000);
handler.postDelayed(task, 5000);
【讨论】:
这应该在 Main 还是 ViewPagerAdapter 上? (我已经包含了上面的代码) @Melfo 在main
但请确保您阅读了处理程序here 的文档【参考方案2】:
假设您的ViewPager
被命名为pager
,您可以使用这样的代码在每次定时器触发时“推进”图像:
int current = pager.getCurrentItem();
int size = pager.getAdapter().getCount();
if (current < (size - 1))
pager.setCurrentItem(current + 1, true);
else
pager.setCurrentItem(0, true);
【讨论】:
【参考方案3】:你的代码逻辑是什么?稍微解释一下并显示代码。这里使用 Handle 和 Timer 完成,每 5 秒(5000 毫秒)执行一次
定义页数变量:
pagesCount = 0;
final Handler handler = new Handler();
final Runnable task = new Runnable()
public void run()
if(pagesCount == 3)
pagesCount = 0;
else
pagesCount++;
ViewPager.setCurrentItem(pagesCount);
;
timer = new Timer();
timer .schedule(new TimerTask()
@Override
public void run()
handler.post(task);
, 0, 5000);
【讨论】:
我已经编辑了帖子并输入了我的代码,非常感谢您的帮助以上是关于如何在确定的时间内从 ViewPager 更改图像?的主要内容,如果未能解决你的问题,请参考以下文章