-1

我制作了一个幻灯片横幅,在 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);
        }
    });
}

我不知道在哪里创建线程以及如何更改位置。

4

3 回答 3

0

使用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);
于 2017-10-31T21:11:44.883 回答
0

假设您ViewPager是 named pager,您可以使用这样的代码在每次计时器触发时“推进”图像:

int current = pager.getCurrentItem();
int size = pager.getAdapter().getCount();

if (current < (size - 1)) {
    pager.setCurrentItem(current + 1, true);
} else {
    pager.setCurrentItem(0, true);
}
于 2017-10-31T21:12:49.483 回答
0

你的代码逻辑是什么?稍微解释一下并显示代码。这里是使用 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);
于 2017-10-31T21:18:58.387 回答