如何在3秒后隐藏视图?

时间:2014-02-24 04:20:04

标签: android handler android-adapterview

我在适配器文件中使用处理程序使视图在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);
}

  }

3 个答案:

答案 0 :(得分:0)

当您需要向已创建的处理程序发送消息时,无需每次都创建新的处理程序。你可以像pagerHandler.sendEmptyMessageDelayed(1, SPLASH_DISPLAY_LENGHT);一样发送延迟信息 您确定,您正在尝试更新当前的shareLayout

答案 1 :(得分: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

答案 2 :(得分:0)

在您的班级文件中添加以下编码。

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

这将有助于您实现自己想要的目标。