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
implements the
The activity will be notified on tab changes (
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.
(2) setOnTouchListener for each tabWidget child in onCreate() of the TabActivity (from MisterSquonk's answer)
但是我自己做了修改,我用的是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();//异步刷新数据的方法,该方法中通过异步获取数据。显示到布局中
}
以上不完善的地方就是点击刷新时的顶部不能出现正在加载字样。但功能已实现,,能够刷新数据并显示
原文是这样说的。。。(此部分代码是Google上的。也就是http://stackoverflow.com/questions/5487770/get-notified-when-current-tab-is-selected-again)
I implemented a
TabActivitywhich
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();//异步刷新数据的方法,该方法中通过异步获取数据。显示到布局中
}
以上不完善的地方就是点击刷新时的顶部不能出现正在加载字样。但功能已实现,,能够刷新数据并显示
相关文章推荐
- 关于使用Jquery Ajax点击checkbox 获取数据库 实现刷新当前页面
- 实现在点击asp:button按钮后,不刷新当前页面
- Android: 如何利用Handler处理和发送消息来实现页面刷新的功能
- js实现表单提交后不重新刷新当前页面
- (三十一)工具方法:获得应用程序包名/安装apk/如何获得当前栈顶的Activity/android如何实现点击一个按钮进入指定网页
- Android listView中点击item或Item中控件跳转对应的详情页面的实现(商品详情查看)
- Android使用TabHost实现在首页tab再次点击首页tab时,刷新首页数据或者recyclerView回到顶部的需求
- 利用cookie实现iframe刷新时停留在当前页面
- Android自定义View之快速实现下拉刷新, 点击加载更多ListView
- jQuery实现的点击图片放大且在当前页面查看原图
- 利用cookie实现iframe刷新时停留在当前页面
- android ViewPager 实现点击小圆点切换页面 案例
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- Android 高仿新浪微博底部导航栏,实现双击首页Tab,页面的ListView滚动、刷新
- 把GridView控件完全放入UpdatePanel中时,实现了点击编辑、更新等按钮时,页面不再刷新,对话框不起作用
- ASP 点击控件刷新后页面样式发生变化后台实现解决方法
- 验证码实现,点击控件不进行页面刷新可参考此文档
- Android ViewPager加Fragment实现滑动或者点击页面切换
- android 点击按钮实现页面跳转并显示以选择信息
- JS实现在线统计一个页面内鼠标点击次数-刷新过后也会保留上次点击次数