I have an android app have four action bar navigation tabs on the top. And I have added fragment to each of them as this suggested. It worked.
// here I created an ActionBar Navigation Tabs from MainActivity
public function initActionBar () {
this.getActionBar().setDisplayShowHomeEnabled(false);
this.getActionBar().setDisplayShowTitleEnabled(false);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String[] tabs = { "test", "test1", "test2", "test3" };
ActionBar.Tab tab = actionBar.newTab().setText(tabs[0]).setTag("0");
tab.setTabListener(new MainTabListener(new Testfragment()));
actionBar.addTab(tab);
ActionBar.Tab tab1 = actionBar.newTab().setText(tabs[1])
.setTag("1");
tab1.setTabListener(new MainTabListener(new Test1Fragment()));
actionBar.addTab(tab1);
}
public class MainTabListener implements TabListener {
ParentListFragment fragment;
boolean added = false;
public MainTabListener(ParentListFragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction ft) {
ft.replace(R.id.jokeFragment, fragment);
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
//ft.detach(fragment);
android.util.Log.v ("main tab listener", "detached");
}
}
Here is main code from TestFragment, and Test1Fragment
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_test, null);
return view;
}
public void init() {
Posts.getInstance().setUpdateInfo();
list = (PullToRefreshListView) this.getActivity().findViewById(
R.id.list2);
list.setTag("first");
list.setOnRefreshListener(new OnRefreshListener<ListView>() {
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
try {
load("0");
}
catch (Exception e) {
e.printStackTrace();
}
}
});
init2();
}
public void init2() {
adapter = new JokeListAdapter(this.getActivity());
adapter.posts = Posts.getInstance().posts0;
list.getRefreshableView().setAdapter(adapter);
footer = (RelativeLayout) ((LayoutInflater) this.getActivity()
.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.view_list_footer, null, false);
footer.setVisibility(LinearLayout.GONE);
loadingMoreLogo = (ProgressBar) footer
.findViewById(R.id.loadingMoreLogo);
loadingMoreLogo.setVisibility(ProgressBar.GONE);
listener = new OnButtonClickListener();
Button button = (Button) footer.findViewById(R.id.buttonLoadMore);
button.setTag("loadmore");
button.setOnClickListener(listener);
button = (Button) this.getActivity().findViewById(R.id.buttonJokeTag);
button.setTag("tag");
button.setOnClickListener(listener);
button = (Button) this.getActivity().findViewById(
R.id.buttonJokeRefresh);
button.setTag("refresh");
button.setOnClickListener(listener);
Posts.getInstance().getDataFromDB(0);
if (Posts.getInstance().posts0 != null) {
adapter.posts = Posts.getInstance().posts0;
adapter.notifyDataSetChanged();
list.getRefreshableView().setAdapter(adapter);
if (list.getTag() == "first") {
list.setTag("");
list.getRefreshableView().addFooterView(footer);
footer.setVisibility(LinearLayout.VISIBLE);
}
}
isNext = false;
load("0");
}
When switch tabs, I found one issue, the Fragment I added to the tab is from ListFragment
, and I will load data while its created as following code, this init function will load all data from service, and update the listView, this time it will scroll to the top of the view. Now when I switched to another tab, and then switched back, this function called again, so the view is jumped to the top. I actually want to know, how to keep the state of the Fragment, so when I switched back, it still in the lastest position it did have. Thanks.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
}
I am looking for an solution avoid to use listView position trick Maintain/Save/Restore scroll position when returning to a ListView, is it able to save fragment state, so next time we can resotre it as it looks like Activity
do? For now, it have to be ActionBar Navigation Tab + Fragments.
This app initalily created fro Android 2.3 and used TabHost, and now I am converting it to Android 4.0 Actionbar based one. In TabHost, it use Activity
for each ListView
, and now I changed them to Fragment. All source codes in github.com, my private repo, if you are willing to help, I can add your account, so you can git cloen it and review it anytime.