在我正在使用的ApplicationActivity onCreate方法中
setContentVIEw(R.layout.application);
设置布局 – 效果很好.
我正在初始化动作栏
Actionbar bar = getSupportActionbar();bar.setNavigationMode(Actionbar.NAVIGATION_MODE_TABS);bar.setdisplayOptions(0,Actionbar.disPLAY_SHOW_Title);bar.setdisplayHomeAsUpEnabled(false);bar.setdisplayShowTitleEnabled(true);// users event List bar.addTab(bar.newTab() .setTag("event_List") .setText(getString(R.string.List_events_header)) .setTabListener(new TabListener<EventListFragment>( this,getString(R.string.List_events_header),EventListFragment.class,null)));
在ApplicationActivity中,我有一个AsyncTask,在初始打开时加载需要几秒钟,并且在手动刷新API时 – 这意味着我需要确保更新上面实例化的片段上的ListVIEw,我在onPostExecute方法,我就是这样做的:
// update the events fragmentEventListFragment fragment = (EventListFragment) getSupportFragmentManager().findFragmentByID(R.string.List_events_header);if(fragment != null) { // restart the loader for this fragment,refreshing the ListvIEw getSupportLoaderManager().restartLoader(0,null,fragment);}
这一切都有效
现在,我想放入一个TabsAdapter来获取花哨的滑动选项卡,我已经完成了它的工作,但我提到的关于onPostExecute的最后一部分,不起作用:(
这是我的TabsAdapter:
package com.lateral.app.ui;import java.util.ArrayList;import androID.content.Context;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentPagerAdapter;import androID.support.v4.app.FragmentTransaction;import androID.support.v4.vIEw.VIEwPager;import com.actionbarsherlock.app.Actionbar;import com.actionbarsherlock.app.Actionbar.Tab;public class APPTabsAdapter extends FragmentPagerAdapter implements Actionbar.TabListener,VIEwPager.OnPagechangelistener { private final Context mContext; private final Actionbar mActionbar; private final VIEwPager mVIEwPager; private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); static final class TabInfo { private final Class<?> clss; private final Bundle args; TabInfo(Class<?> _class,Bundle _args) { clss = _class; args = _args; } } public APPTabsAdapter(ApplicationActivity activity,VIEwPager pager) { super(activity.getSupportFragmentManager()); mContext = activity; mActionbar = activity.getSupportActionbar(); mVIEwPager = pager; mVIEwPager.setAdapter(this); mVIEwPager.setonPagechangelistener(this); } public APPTabsAdapter(EventActivity activity,VIEwPager pager) { super(activity.getSupportFragmentManager()); mContext = activity; mActionbar = activity.getSupportActionbar(); mVIEwPager = pager; mVIEwPager.setAdapter(this); mVIEwPager.setonPagechangelistener(this); } public voID addTab(Actionbar.Tab tab,Class<?> clss,Bundle args) { TabInfo info = new TabInfo(clss,args); tab.setTag(info); tab.setTabListener(this); mTabs.add(info); mActionbar.addTab(tab); notifyDataSetChanged(); } @OverrIDe public int getCount() { return mTabs.size(); } @OverrIDe public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext,info.clss.getname(),info.args); } public voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } public voID onPageSelected(int position) { mActionbar.setSelectednavigationItem(position); } public voID onPageScrollStateChanged(int state) { } public voID onTabSelected(Tab tab,FragmentTransaction ft) { Object tag = tab.getTag(); for (int i = 0; i < mTabs.size(); i++) { if (mTabs.get(i) == tag) { mVIEwPager.setCurrentItem(i); } } } public voID onTabUnselected(Tab tab,FragmentTransaction ft) { } public voID onTabReselected(Tab tab,FragmentTransaction ft) { }}
这是我从ApplicationActivity更新的onCreate方法
mVIEwPager = new VIEwPager(this);mVIEwPager.setID(R.ID.pager);setContentVIEw(mVIEwPager);Actionbar bar = getSupportActionbar();bar.setNavigationMode(Actionbar.NAVIGATION_MODE_TABS);bar.setdisplayOptions(0,Actionbar.disPLAY_SHOW_Title);bar.setdisplayHomeAsUpEnabled(false);bar.setdisplayShowTitleEnabled(true);mTabsAdapter = new APPTabsAdapter(this,mVIEwPager);// users event List mTabsAdapter.addTab(bar.newTab() .setTag("event_List") .setText(getString(R.string.List_events_header)) .setTabListener(new TabListener<EventListFragment>( this,null)),null);
这是我对ApplicationActivity中的onPostExecute方法所做的更新
// update the events fragmentEventListFragment fragment = (EventListFragment) mTabsAdapter.getItem(0);if(fragment != null) { // restart the loader for this fragment,fragment);}
基本上,当它试图运行我的onPostExecute代码时
我从片段中的cursorAdapter得到一个NullPointerException但它找到了片段..
编辑 – 请求的代码
public Loader<Cursor> onCreateLoader(int loaderID,Bundle args) { Log.d(TAG,"onCreateLoader"); return new EventCursorLoader(getActivity());}public static final class EventCursorLoader extends SimpleCursorLoader { Context mContext; public EventCursorLoader(Context context) { super(context); Log.d("EventCursorLoader","Constructor"); mContext = context; } @OverrIDe public Cursor loadInBackground() { Log.d("EventCursorLoader","loadInBackground"); EventsDataSource datasource = new EventsDataSource(mContext,((ApplicationActivity)mContext).getDbHelper()); SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(mContext); long userID = app_preferences.getLong("authenticated_user_ID",0); return datasource.getAllEvents(userID); }}解决方法 试图在这里检索加载器只是错误的想法,因为目的是刷新数据.在做了一些挖掘之后,我发现我可以在我的提供商中使用通知超过1组内容
getContext().getContentResolver().notifyChange(uri,null);
通知它更改和刷新,所有这些代码都可以删除并替换为一行.
总结以上是内存溢出为你收集整理的带有ActionbarSherlock的Android TabsAdapter全部内容,希望文章能够帮你解决带有ActionbarSherlock的Android TabsAdapter所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)