2014-02-24 82 views
0

我正在使用適配器文件中的處理程序使視圖在3秒後不可見,但它不更新視圖。我的問題在哪裏,我該如何解決?如何在3秒後隱藏視圖?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    try { 

     Bundle bun = getIntent().getExtras(); 
     if (!bun.isEmpty()) { 
      selecte_pos = bun.getInt("pos"); 

     } 
    } catch (Exception e) { 
    } 

    Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth(); 
    height = display.getHeight(); 

    System.out.println("full image activity"); 

    ar = getIntent().getStringArrayListExtra("PhotoList"); 

    System.out.println("full list size: " + ar.size()); 

    imageLoader = new ImageLoader(this); 
    setContentView(R.layout.full_image_view_layout); 
    mPager = (ViewPager) findViewById(R.id.full_image_view_pager); 
    adapter = new MyPagerAdapter(); 
    mPager.setAdapter(adapter); 
     mPager.setCurrentItem(selecte_pos); 
} 

class MyPagerAdapter extends PagerAdapter { 

    public int getCount() { 

     return ar.size(); 
    } 
    Handler pagerHandler=new Handler(){ 


     public void handleMessage(Message msg){ 
      if(msg.what==1){ 
      //if (istimer == true) { 
       System.out.println("page handler"); 

       System.out.println(""); 
       Toast.makeText(getApplicationContext(), "hi", 1).show(); 
       shareLayout.setVisibility(View.INVISIBLE); 
       adapter.notifyDataSetChanged(); 

      //} 


      } 
     } 
    }; 
    @Override 
    public Object instantiateItem(ViewGroup container, final int position) { 

     System.out.println("view page clciking"); 

     LayoutInflater layoutInflater = (LayoutInflater) container 
       .getContext().getSystemService(LAYOUT_INFLATER_SERVICE); 

     View v = layoutInflater.inflate(R.layout.imageview, null); 
     try { 
      final ImageView imageView = (ImageView) v 
        .findViewById(R.id.full_pager_adapter_iv); 

      shareLayout = (LinearLayout) v.findViewById(R.id.buttonslayout); 
      shareLayout.setVisibility(View.VISIBLE); 

      imageLoader.DisplayImage(ar.get(position), imageView); 





      if (first) { 
       System.out.println("fisr condition"); 
       first = false; 


        new Handler().postDelayed(new Runnable() { 
         @Override 
         public void run() { 


          pagerHandler.sendEmptyMessage(1); 
          //adapter.notifyDataSetChanged(); 
         } 
        }, SPLASH_DISPLAY_LENGHT); 

       } 



      /**if(istimer1== true) 
      { 
       System.out.println("else part"); 

       istimer1 = false; 
       shareLayout.setVisibility(View.VISIBLE); 
       adapter.notifyDataSetChanged(); 
      }**/ 

      imageView.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        System.out.println("img clicking"); 

        if (shareLayout.getVisibility() == View.INVISIBLE) { 
         System.out.println("invisible"); 
         istimer = false; 
         istimer1 = true; 
         shareLayout.setVisibility(View.VISIBLE); 
         new Handler().postDelayed(new Runnable() { 
         @Override 
         public void run() { 

          //istimer = true; 
          //istimer1 = false; 
          pagerHandler.sendEmptyMessage(1); 
          //adapter.notifyDataSetChanged(); 
         } 
        }, SPLASH_DISPLAY_LENGHT); 



         adapter.notifyDataSetChanged(); 

        } 

        if (shareLayout.getVisibility() == View.VISIBLE) { 
         System.out.println("visible"); 
         istimer = true; 
         istimer1 = false; 
         shareLayout.setVisibility(View.INVISIBLE); 

         adapter.notifyDataSetChanged(); 
        } 

       } 
      }); 

      LinearLayout btnshare = (LinearLayout) v 
        .findViewById(R.id.btnshare); 
      btnshare.setTag(position); 
      btnshare.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        int pos = (Integer) v.getTag(); 

        String s = ar.get(pos); 
        // String s = bean.getImagePath(); 
        File file = new File(s); 

        Intent sendMailIntent = new Intent(Intent.ACTION_SEND); 
        sendMailIntent.setType("image/jpeg"); 

        sendMailIntent.putExtra(
          android.content.Intent.EXTRA_TEXT, ""); 
        sendMailIntent.putExtra(Intent.EXTRA_STREAM, 
          Uri.fromFile(file)); 
        /* sendMailIntent.setType("image/*"); */ 
        startActivity(Intent.createChooser(sendMailIntent, "")); 
       } 
      }); 

     } catch (Exception e) { 

     } 

     ((ViewPager) container).addView(v, 0); 

     return v; 

    } 

    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view) { 

     ((ViewPager) collection).removeView((View) view); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == object; 
    } 

    @Override 
    public int getItemPosition(Object object) { 
     // TODO Auto-generated method stub 

     return POSITION_NONE; 
    } 

} 

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    finish(); 
} 

private Handler mTimerHandler = new Handler(); 

private void stopTimer() { 
    if (mTimer1 != null) { 
     mTimer1.cancel(); 
     mTimer1.purge(); 
    } 
} 

private void startTimer() { 
    mTimer1 = new Timer(); 
    mTt1 = new TimerTask() { 
     public void run() { 
      mTimerHandler.post(new Runnable() { 
       public void run() { 

        System.out.println("timesatrs"); 
        // TODO 
        if (shareLayout.getVisibility() == View.VISIBLE) { 
         System.out.println("time completed"); 
         shareLayout.setVisibility(View.INVISIBLE); 
        } 

       } 
      }); 
     } 
    }; 

    mTimer1.schedule(mTt1, 6000); 
} 

    } 

回答

0

當您需要將消息發送到您已創建的處理程序時,您不需要每次創建新的處理程序。您可以發送延遲信息,如:pagerHandler.sendEmptyMessageDelayed(1, SPLASH_DISPLAY_LENGHT); 您確定,您正在嘗試更新當前shareLayout

+0

是的,我想更新shareLayout – user3114723

+0

確保你正在更新正確的佈局,也是從多個點更新,嘗試從一次更新,然後繼續你的邏輯。 – Yuvi

0
getWindow().getDecorView().getRootView().postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        yourView.setVisibility(View.VISIBLE); 
       } 
      }, 5000); 

// Call this when u initalise view in onCreate 
0

將下面的代碼添加到您的類文件中。

new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       view.setVisibility(View.GONE); } 
    }, 3000); 

這將幫助你實現你想要的。