我有一个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]);
}
}
}
答案 0 :(得分:1)
首先,永远不要像你一样用Thread.sleep()
来阻止UI线程。通过使用Thread.sleep()
,您基本上会为对话框设置show命令(在<{1}}方法返回>>之后会发生),睡眠一秒(并且您的应用将会冻结)然后在onCLick()
上设置页面,这将触发监听器,解除对话框。相反,您可以使用ViewPager
Handler