-1
我有一個ViewPager
,並讓想象,我是第0頁顯示對話框取決於ViewPager狀態
我有這個頁面上的一個按鈕上,並單擊此按鈕,我想顯示一個對話框,並將頁面切換到頁面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]);
}
}
}
您是否介意發佈您當前使用的代碼? – Luksprog
@Luksprog,完成。 – ArieDov