0

我有一个ViewPager,让我们想象一下,我在第 0 页。

我在这个页面上有一个按钮,在这个按钮上单击,我想显示一个对话框,并将页面更改为第 1 页。

当我的页面更改为第 1 页时,我希望对话框消失。

当我这样做时,我没有看到对话框,它在页面更改时出现和消失,但我确信,我在此操作之间有 1000 毫秒的延迟。

请帮忙,我怎样才能显示对话框?

package com.example.ViewPagerDialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MyActivity extends Activity {
    private int currentPage;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final MyViewPager viewPager = (MyViewPager) findViewById(R.id.view_pager);
        final Button leftSwitcher = (Button) findViewById(R.id.left_switcher);
        final Button rightSwitcher = (Button) findViewById(R.id.right_switcher);
        final ProgressDialog progressDialog = new ProgressDialog(this);

        leftSwitcher.setVisibility(View.GONE);
        progressDialog.setTitle("Wait...");

        viewPager.setAdapter(new MyPagerAdapter(this));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrolled(int i, float v, int i2) {

            }

            public void onPageSelected(int i) {
                progressDialog.dismiss();

                currentPage = i;

                if (i == 0) {
                    leftSwitcher.setVisibility(View.GONE);
                } else if (i == 1) {
                    leftSwitcher.setVisibility(View.VISIBLE);
                    rightSwitcher.setVisibility(View.VISIBLE);
                } else if (i == 2) {
                    rightSwitcher.setVisibility(View.GONE);
                }
            }

            public void onPageScrollStateChanged(int i) {

            }
        });

        leftSwitcher.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                progressDialog.show();
                sleepThread();
                viewPager.setCurrentItem(currentPage - 1);
            }
        });

        rightSwitcher.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                progressDialog.show();
                sleepThread();
                viewPager.setCurrentItem(currentPage + 1);
            }
        });
    }

    private void sleepThread() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class MyPagerAdapter extends PagerAdapter {
        View[] views = new View[3];

        public MyPagerAdapter(Context context) {
            TextView view1 = new TextView(context);
            TextView view2 = new TextView(context);
            TextView view3 = new TextView(context);

            view1.setText("View 1");
            view2.setText("View 2");
            view3.setText("View 3");

            views[0] = view1;
            views[1] = view2;
            views[2] = view3;
        }

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

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return (view.equals(o));
        }

        @Override
        public Object instantiateItem(ViewGroup collection, int position) {
            collection.addView(views[position]);
            return views[position];
        }

        @Override
        public void destroyItem(android.view.ViewGroup container, int position, java.lang.Object object) {
            container.removeView(views[position]);
        }
    }
}
4

1 回答 1

1

首先,永远不要Thread.sleep()像你一样阻塞 UI 线程。通过使用Thread.sleep(),您将基本上设置对话框的显示命令(这将在您从方法返回onCLick()发生),休眠一秒钟(您的应用程序将冻结),然后设置ViewPager将触发侦听器的页面,关闭对话。相反,您可以使用Handlerto

private Handler mHandler = new Handler();

// in the onClick method
progressDialog.show();
mHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
        viewPager.setCurrentItem(currentPage - 1);
    }

}, 1000);
于 2013-07-30T08:59:53.170 回答