2015-06-14 61 views
0

我創建了幻燈片,其中我已禁用默認滑動操作以滑動屏幕。而當我點擊幻燈片執行的圖像時。但是我想在圖片被點擊後延遲幻燈片t毫秒。點擊動作的方法是onClickSlideDown。 的viewpager類如下:使用viewpager單擊圖像時延遲滑動操作android

public class slidescreen extends ActionBarActivity implements Animation.AnimationListener { 

//Declare variables 
ViewPager viewPager; 
PagerAdapter adapter; 
int[] background; 
int[] icon; 
String[] title; 
String[] title_2; 
String[] description; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.slidescreenmain); 

    //Generate sample data 
    background = new int[]{R.mipmap.bg1, R.mipmap.bg2, R.mipmap.bg3, R.mipmap.bg4, R.mipmap.bg5, R.mipmap.bg6, 
      R.mipmap.bg7, R.mipmap.bg8, R.mipmap.bg9, R.mipmap.bg10, R.mipmap.bg11, R.mipmap.bg12}; 

    icon = new int[]{R.mipmap.im1, R.mipmap.im2, R.mipmap.im3, R.mipmap.im4, R.mipmap.im5, 
      R.mipmap.im6, R.mipmap.im7, R.mipmap.im8, R.mipmap.im9, R.mipmap.im10, R.mipmap.im11, R.mipmap.im12}; 

    title = new String[]{"ALTA RESISTENCIA A", "ALTA RESISTENCIA", "ALTAMENTE", "RESISTENCIA A", "MATERIAL", "ALTA RESISTENCIA", 
      "RESISTENCIA AL", "RESISTENCIA", "ESTABILIDAD", "ESTABILIDAD", "RESISTENCIA A", "NULA ABSORCIÓN"}; 

    title_2 = new String[]{"LOS RAYOS UV", "AL FUEGO Y AL CALOR", "RESISTENTE AL RAYADO", "LAS MANCHAS", "INCOMBUSTIBLE", "A LA HIDRÓLISIS", 
      "HIELO Y DESHIELO", "MECÁNICA", "DIMENSIONAL", "DEL COLOR", "LA ABRASIÓN", "DEL AGUA"}; 

    description = new String[]{"Por naturaleza, es capaz del repeler\n" + "líquidos y gases para que no penetren en\n" + 
        "la superficie. De este modo, el\n" + "mantenimiento de la superficie es mínimo\n" + 
        "y más fácil de limpiar."}; 



    // Locate the ViewPager in viewpager_main.xml 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    // Pass results to ViewPagerAdapter Class 
    adapter = new ViewPagerAdapter(slidescreen.this, background, icon, title, title_2, description); 
    // Binds the Adapter to the ViewPager 
    viewPager.setAdapter(adapter); 

    getSupportActionBar().hide(); 
} 


public void onClickSlideDown(View view) { 

    Animation slideback; 
    ImageView iconimage, whitebox; 
    TextView titletext, title_2text, descriptiontext; 
    titletext = (TextView)findViewById(R.id.title); 
    title_2text = (TextView)findViewById(R.id.title_2); 
    descriptiontext = (TextView)findViewById(R.id.description); 
    iconimage = (ImageView)findViewById(R.id.icon); 
    whitebox = (ImageView)findViewById(R.id.whitebox); 
    slideback = AnimationUtils.loadAnimation(this, R.anim.whiteboxanimback); 
    slideback.setAnimationListener(this); 
    whitebox.startAnimation(slideback); 
    iconimage.startAnimation(slideback); 
    titletext.startAnimation(slideback); 
    title_2text.startAnimation(slideback); 
    descriptiontext.startAnimation(slideback); 

    if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount()) { 
     viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true); 
    } else { 
     Intent i1 = new Intent(this, glass_3.class); 
     startActivity(i1); 
    } 

謝謝:)

回答

0

嘗試了這一點。將此代碼添加到onClickSlideDown方法中。

Thread timer = new Thread(){ 

       public void run(){ 
        try{        
         sleep(2000);        
        }catch(InterruptedException e){       
         e.printStackTrace();        
        }finally 
        {       

         //Your entire code of onClickSlideDown method      
        } 
       } 
      }; 
      timer.start(); 

希望這會工作,並延遲2000毫秒的幻燈片。

UPDATE:如果在意圖導致錯誤的關鍵字「this」中,嘗試使用getApplicationContext()而不是intent中的this關鍵字。

+0

我有我的一個問題t ..我應該寫下面的代碼來代替「this」: Intent i1 = new Intent(** this **,glass_3.class); 對不起,我不知道Java很多。謝謝@lsh –

+0

這是錯誤的是過度複雜。使用postDelayed() –

0

感謝@Ish他的回答,但你不能對任何其他線程做UI操作...

所以處理程序來搶救,其中默認的構造採用主線程消息隊列尺蠖對象

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      //TODO: Write your code here 
     } 
    }, delayinMiils); 

但是當你添加耽誤你在玩主線程的上下文中泄漏......從而避免任何形式的情況下泄漏的請參考下面的實施

private final MyHandler mHandler = new MyHandler(this); 

    /** 
    * Instances of anonymous classes do not hold an implicit 
    * reference to their outer class when they are "static". 
    */ 
    private static final Runnable sRunnable = new Runnable() { 
     @Override 
     public void run() { /* ... */ } 
    }; 

    // Post a message and delay its execution for 10 minutes. 
    mHandler.postDelayed(sRunnable, 1000 * 60 * 10);