您的位置:首页 > 移动开发 > Android开发

android 点击tab页实现当前页面的刷新

2013-11-19 15:32 337 查看
最近要实现一个像新浪微博的点击首页实现当前页面的刷新。在网上看了好多文章,最终还是在Google上的http://stackoverflow.com/questions/5487770/get-notified-when-current-tab-is-selected-again得到了启发,终于能够实现此功能,现在就把实现此功能的做法。。

原文是这样说的。。。(此部分代码是Google上的。也就是http://stackoverflow.com/questions/5487770/get-notified-when-current-tab-is-selected-again)

I implemented a 
TabActivity
 which
implements the 
OnTabChangeListener
.
The activity will be notified on tab changes (
onTabChanged(String
tabId)
).

Is it also possible to get notified if the user selects the current tab again?

I would like to use this event to perform a "refresh" of the current tab content instead of providing a refresh button on the tab or in the options menu.

That's the way I finally solved the problem - solution hint was in MisterSquonk answer.

(1) Define a OnTabReselectListener which must be implemented by an activity which represents a tab content and which will be notified on reselect events.

/**
* Interface definition for a callback to be invoked when a current selected tab
* in a TabHost is selected again.
*/
public interface OnTabReselectListener {

/**
* Called when a current visible tab is selected again. Will not be invoked
* on tab changes.
*/
void onTabReselect();

}

(2) setOnTouchListener for each tabWidget child in onCreate() of the TabActivity (from MisterSquonk's answer)

for (int i = 0; i < tabWidget.getChildCount(); i++) {
View v = tabWidget.getChildAt(i);
v.setOnTouchListener(this);
}
(3) Implement OnTouchListener.onTouch() in the TabActivity. Do some logic to decide if the current tab was selected again and notifiy the tab's activity.
/**
* @see android.view.View.OnTouchListener#onTouch(android.view.View,
*      android.view.MotionEvent)
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean consumed = false;
// use getTabHost().getCurrentTabView to decide if the current tab is
// touched again
if (event.getAction() == MotionEvent.ACTION_DOWN
&& v.equals(getTabHost().getCurrentTabView())) {
// use getTabHost().getCurrentView() to get a handle to the view
// which is displayed in the tab - and to get this views context
View currentView = getTabHost().getCurrentView();
Context currentViewContext = currentView.getContext();
if (currentViewContext instanceof OnTabReselectListener) {
OnTabReselectListener listener = (OnTabReselectListener) currentViewContext;
listener.onTabReselect();
consumed = true;
}
}
return consumed;
}


但是我自己做了修改,我用的是tab选项卡布局。。在TabActivity中定义一个接口。

 //用于实现点击首页按钮的接口

  public interface OnTabReselectListener

  {

    public void onTabReselect();

  }

在Tabactivity的onCreate()方法中

    mainTab = (RadioGroup) findViewById(R.id.main_radio);

    mainTab.getChildAt(0).setOnClickListener(this);//首页按钮点击事件

在TabActivity实现OnClickListener

 @Override

  public void onClick(View v)

  {

   //这两行代码的具体含义正在研究

    View currentView = getTabHost().getCurrentView();

    Context currentViewContext = currentView.getContext();

    if (currentViewContext instanceof OnTabReselectListener)

    {

      OnTabReselectListener listener = (OnTabReselectListener) currentViewContext;

      listener.onTabReselect();

    }

  }

在你想实现刷新的Activity中实现OnTabReselectListener,重写onTabReselect()

 // 实现动态按钮的点击事件

  @Override

  public void onTabReselect()

  {

    listInfo.setSelection(0);//使得当前的listview回到顶部

    refreshInfoData();//异步刷新数据的方法,该方法中通过异步获取数据。显示到布局中

  }

以上不完善的地方就是点击刷新时的顶部不能出现正在加载字样。但功能已实现,,能够刷新数据并显示



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐