问题描述
我有一个问题。我有一个活动与ViewPager其中有一些片段。
I have a problem. I have an Activity with ViewPager which have some Fragments.
在我的第一个片段,我有一些元素。比方说一个ImageView的,TextView的。如何让他们通过定时器可见,当你完全打开ViewPager的页面?!
In my first Fragment I have some elements. Lets say an ImageView, TextView. How to make them visible by timer when you completely open the page of ViewPager?!
我用下面这个code在我MainActivity.java文件做出的ImageView由定时器可见,但它的工作原理并不如我所料。当我要到下一个页面滚动和触摸屏幕ImageView的dissapear。为什么它的发生?!
I used this code below in my MainActivity.java document to make that ImageView visible by timer but it works not as I expected. When i want to go to next page by scrolling and touch the screen ImageView dissapear. Why its happened?!
final ImageView mImageView = (ImageView) findViewById(R.id.earth);
mImageView.setVisibility(View.INVISIBLE);
mImageView.postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
}}, 5000);
任何帮助是AP preciated。谢谢!
Any help is appreciated. Thanks!
我的新编辑:
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
//open concrete page of ViewPager. setCurrentItem(index of page)
pager.setCurrentItem(1);
tabs.setOnPageChangeListener(new OnPageChangeListener() {
//This method will be invoked when the current page is scrolled, either as part of a program initiated smooth scroll or a user initiated touch scroll.
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {
final ImageView mImageView = (ImageView) findViewById(R.id.earth);
mImageView.setVisibility(View.INVISIBLE);
if (position == 0) {
mImageView.postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
}
}, 5000);
}
}
//Called when the scroll state changes.
@Override
public void onPageScrollStateChanged(int state) {
}
});
我的问题是有标签和ViewPager冲突。当我使用tabs.setOnPageChangeListener(新OnPageChangeListener()我有pager.setCurrentItem冲突(1);这显示了当活动第一次打开具体的页面在我的情况下,其显示的2-D页活性先开,但选项卡显示打开1时。 -st页。为什么?!如何解决这个问题呢?!
My Problem is conflict with TABS and ViewPager. When I use tabs.setOnPageChangeListener(new OnPageChangeListener() i have conflict with pager.setCurrentItem(1); which shows concrete page when Activity first opened. In my case its shows 2-d page when Activity first opened but Tab shows that opened 1-st page. WHY?! How to solve this problem?!
推荐答案
我不会用一个定时器。它要么火得太早,或者太晚了你的用户界面很糟糕其间。使用寻呼机的事件,知道什么时候更新你的用户界面。例如,你也许可以使用 OnPageChangedListener
:
I would not use a timer. It would either fire too early, or too late during which your UI looks bad. Use the pager's events to know when to update your UI. For example, you can probably use OnPageChangedListener
:
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// Now displaying page at position
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
这篇关于如何显示由定时器元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!