Android - Expandable List within Fragments - android

Hey, I am trying to convert my existing App using Tabhost and Activities to a Action Bar based App with fragments. In this process a ran into the issue to implement Expandable Lists.
I haven´t found any simple example in the internet on how to implement this Expandable List. As ExpandableListActivity has no counterpart for fragments I think I need to use and extend ListFragment but as I am quite new to android I haven´t got any clue how. :)
I would be glad if someone could shed some light on how to use an expandable list with fragments.
Thanks.
Attached is also a snippet of my existing code.
Fragment 1 with two spinners already
public class Configurator_Fragment extends SherlockListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
final View v = inflater.inflate(R.layout.configurator_layout,
container, false);
final Spinner spinner = (Spinner) v
.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.array1,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
getActivity(), R.array.array2,
android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
return v;
}
}
FragmentActivity
public class TabActivity extends SherlockFragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
if (savedInstanceState == null) {
Fragment newFragment = new Configurator_Fragment();
Fragment newFragment2 = new SecondFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.pager, newFragment, "First");
ft.add(R.id.pager, newFragment2, "Second");
ft.commit();
}
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");
mViewPager = (ViewPager)findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
mTabsAdapter.addTab(tab1, Configurator_Fragment.class, null);
mTabsAdapter.addTab(tab2, SecondFragment.class, null);
if (savedInstanceState != null) {
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());
}
public static class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private FragmentTransaction mCurTransaction = null;
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mBar = bar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mBar.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);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
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);
}
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}

A couple disclaimers first...
1) I haven't used ABS, so I'm not sure what changes that would make.
2) My ExpandableListView is backed by a database, not arrays. I say this becasue it looks like you are using arrays. I post this anyway in the hopes it might still provide you with some direction.
That said, I've done it simply by extending Fragment.
public class EventListFragment extends Fragment {
private AttendanceDB mDbHelper;
public static Cursor mGroupsCursor;
private MyExpandableListAdapter mAdapter;
private String mGroup;
private String mChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.explistfragment, container, false);
header = (TextView) v.findViewById(R.id.header1);
header.setText(R.string.event_header);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle extras = getIntent().getExtras();
mGroup = extras.getString("Group");
mChild = extras.getString("Child");
mDbHelper = new AttendanceDB(getActivity());
mDbHelper.open();
fillData(mGroup, mChild);
}
private void fillData(String group, String child) {
ExpandableListView lv;
mGroupsCursor = mDbHelper.fetchGroup(group);
getActivity().startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();
lv = (ExpandableListView) getActivity().findViewById(R.id.explist);
mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
R.layout.explistlayout,
R.layout.explistlayout1,
new String[] { "_id" },
new int[] { android.R.id.text1 },
new String[] { child },
new int[] { android.R.id.text1 });
lv.setAdapter(mAdapter);
}
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
public MyExpandableListAdapter(Cursor cursor, Context context,
int groupLayout, int childLayout, String[] groupFrom,
int[] groupTo, String[] childrenFrom, int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
#Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Cursor childCursor = mDbHelper.fetchChildren(mGroup, groupCursor
.getString(groupCursor
.getColumnIndex(AttendanceDB.EVENT_ROWID)));
getActivity().startManagingCursor(childCursor);
childCursor.moveToFirst();
return childCursor;
}
}
}

Related

Change data in listview at fragment

I have create a listview in a fragment in a tab in Android 2.3. I want to change the data in listview when I click the tab. For example, in "tab1" I can see "1,2,3" in listview, then I click to "tab2" and click back to "tab1", the listview change to show "a,b,c". I have create a refresh() method, and call it in onPageSelected, but it always got NullPointerException.
Here is my code:
List_View
public class List_View extends ListFragment{
protected ArrayAdapter listAdapter;
private ArrayList<String> items = new ArrayList<String>();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.list, container, false);
items.add("1");
items.add("2");
items.add("3");
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
setListAdapter(listAdapter);
return rootView;
}
public void refresh(){
items.clear();
listAdapter.notifyDataSetChanged();
items.add("a");
items.add("b");
items.add("c");
listAdapter.notifyDataSetChanged();
}
}
TabsAdapter
public class TabsAdapter extends FragmentStatePagerAdapter implements TabListener, OnPageChangeListener{
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "";
private List_View list_view;
static final class TabInfo{
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args){
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = bar;
mViewPager = pager;
mViewPager.setOffscreenPageLimit(0);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(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 void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
list_view.refresh();
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i = 0; i<mTabs.size(); i++){
if (mTabs.get(i) == tag){
mViewPager.setCurrentItem(i);
}
}
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
#Override
public int getCount() {
return mTabs.size();
}
}
I have create a refresh() method, and call it in onPageSelected, but
it always got NullPointerException
You have
private List_View list_view;
And you have
list_view.refresh();
But i don't see any code that initialized list_view.
Fragment to Fragment communication is done through the associated Activity.
http://developer.android.com/training/basics/fragments/communicating.html
So use a interface as a callback to the activity from fragment in tab2 and then communicate to List_View and change the list accordingly.

RuntimeException on SherlockFragment

I'm following this example.. https://bitbucket.org/owentech/abstabsviewpager/src
I tried implementing setOnClickListener for Button in OnCreatemethod and its getting force close.
Note: Without implementing Button its working fine..
code:
public class ABSTabsViewPagerActivity extends SherlockFragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
b = (Button) findViewById(R.id.buttonA);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button Clicked",
Toast.LENGTH_SHORT).show();
}
});
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 1"),
FragmentOne.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 2"),
FragmentTwo.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 3"),
FragmentThree.class, null);
}
public static class TabsAdapter 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 TabsAdapter(SherlockFragmentActivity 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) {
}
}
}
Your setContentView(mViewPager) have only viewpager. so there is no button in the present layout. you can't get button like this b = (Button) findViewById(R.id.buttonA);
set the full layout which has button also in the xml.
It's obvious that the Button, that you're trying to reference by calling findViewById(), is not inside the root layout, because the root View is a ViewPager. You should create a layout where both the ViewPager and the Button are present. Hope this helps.

ListFragment inside ViewPager

I have a Viewpager, that contain fragments, and a listfragment, the listfragment gets data from a asynctask, and then uses the setListAdapter to set the data, the only problem is, the data is not showing. Does anyone have a suggestion to address this problem?
public class AndroidViewPagerActivity extends FragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Tab1"),
FragmentB.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Tab2"),
FragmentA.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Tab3"),
FragmentC.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 1));
}
mViewPager.setCurrentItem(1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem2:
Toast.makeText(getApplicationContext(), "menu klikket!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
public static class TabsAdapter 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 TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
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();
}
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
public void onPageSelected(int position) {
// TODO Auto-generated method stub
mActionBar.setSelectedNavigationItem(position);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
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) {
// TODO Auto-generated method stub
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(),
info.args);
}
#Override
public int getCount() {
return mTabs.size();
}
}
And a Class FragmentA
public class FragmentA extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragmenta,
container, false);
return myFragmentView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v("ListFragment", "onActivityCreated().");
Log.v("ListsavedInstanceState", savedInstanceState == null ? "true"
: "false");
// Generate list View from ArrayList
new GetList().execute();
}
#Override
public void onStart() {
super.onStart();
}
class GetList extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(
getActivity());
//Data is retrieved from a server, and added to the ListAdapter
ListAdapter adapter = new SimpleAdapter(getActivity(), contactList,
R.layout.fragmenta, new String[] { TAG_NAME, TAG_UID,
TAG_START }, new int[] { R.id.email_label,
R.id.mobile_label, R.id.mobile_label });
setListAdapter(adapter);
Log.e("LIST", "setListAdapter is done");
}
}
My code runs fine, but the list is not showing. Can someone please help me with this?

refresh viewpager, lsitfragment or adapter?

i have a main class that hold a actionbarsherlock with tabs and viewpager
here is it:
public class HSG_Handball extends SherlockFragmentActivity
{
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Alle Spiele"), Spiele.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Tabelle"), Tabelle.class, null);
}
public static class TabsAdapter 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 TabsAdapter(SherlockFragmentActivity 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)
{
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
SubMenu subMenu1 = menu.addSubMenu("Aktive");
subMenu1.add("Herren 1");
subMenu1.add("Herren 2");
subMenu1.add("Herren 3");
subMenu1.add("Damen 1");
subMenu1.add("Damen 2");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu subMenu2 = menu.addSubMenu("Jugend");
subMenu2.add("männl. A");
subMenu2.add("weibl. A");
subMenu2.add("männl. B1");
subMenu2.add("männl. B2");
subMenu2.add("männl. C");
subMenu2.add("männl. D");
subMenu2.add("E-Jugend");
MenuItem subMenu2Item = subMenu2.getItem();
subMenu2Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(!item.getTitle().toString().equals("Active") || !item.getTitle().toString().equals("Jugend"))
{
new Spiele().FormatiereSpieleSeite(item.getTitle().toString());
}
return super.onOptionsItemSelected(item);
}
}
the second class (Spiele) is a listfragment with a custom base adapter.
The class looks so:
public class Spiele extends ListFragment
{
String ligaId = "0";
SpieleAdapterFuerCustomList adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.spiele, container, false);
List<String> dateAndSchiri = new ArrayList<String>();
dateAndSchiri.add("DATUM · Schiri 1 : Schiri 2");
dateAndSchiri.add("DATUM · Schiri 1 : Schiri 2");
dateAndSchiri.add("DATUM · Schiri 1 : Schiri 2");
List<String> heim = new ArrayList<String>();
heim.add("HSG Nieder-Roden");
heim.add("SG Bruchköbel");
heim.add("LOL Idiotenklupp");
List<String> gast = new ArrayList<String>();
gast.add("KG Wallau");
gast.add("DJ G4bby");
gast.add("Hanzup Motherf*ckers");
adapter = new SpieleAdapterFuerCustomList(getActivity(), dateAndSchiri, heim, gast);
setListAdapter(adapter);
return view;
}
public void FormatiereSpieleSeite(String mannschaft)
{
// aktive
if(mannschaft.equals("Herren 1"))
ligaId = "X0";
if(mannschaft.equals("Herren 2"))
ligaId = "00151350asd0";
if(mannschaft.equals("Herren 3"))
ligaId = "00151asdad000000000000000001000";
if(mannschaft.equals("Damen 1"))
ligaId = "001513asdad0000000007000";
if(mannschaft.equals("Damen 2"))
ligaId = "0015135asd0000000000003000";
// jugend
if(mannschaft.equals("männl. A"))
ligaId = "0015135asdasd0000000000001000";
if(mannschaft.equals("weibl. A"))
ligaId = "";
if(mannschaft.equals("männl. B1"))
ligaId = "001513asdasd0000000000000002000";
if(mannschaft.equals("männl. B2"))
ligaId = "00151350asd05502000000000000001000";
if(mannschaft.equals("männl. C"))
ligaId = "0015asdasd503000000000000001000";
if(mannschaft.equals("männl. D"))
ligaId = "001513504501asdsad0000001000";
if(mannschaft.equals("E-Jugend"))
ligaId = "0015135asdasd000000000003000";
System.out.println(ligaId + "Das ist liga");
List<String> dateAndSchiri = new ArrayList<String>();
dateAndSchiri.add("DATUM as Schiri 2");
dateAndSchiri.add("DATUMasas Schisdfsdfri 2");
dateAndSchiri.add("DATUMasas Schirsdfsfi 2");
List<String> heim = new ArrayList<String>();
heim.add("HSG Nieder-sdfRoden");
heim.add("SG Brucsdfhköbel");
heim.add("LOL Idiosdfsdftenklupp");
List<String> gast = new ArrayList<String>();
gast.add("KG Walsdfsflau");
gast.add("DJ G4sdfsfbby");
gast.add("Hanzusdfsfp Motherf*ckers");
adapter = new SpieleAdapterFuerCustomList(getActivity(), dateAndSchiri, heim, gast);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
All is ok and run fine.
On the "main"-class. The class that holds the viewpager and tabsactionbar have menus. If the user click on one of them it must load a new ListFragment. Or a new BaseAdapter(?).
I want that the ListFragment (class: Spiele) is updatet.
But how can i do that?
How update my ListFragment?
Why this ArrayList with TabInfo? Why not use an ArrayList with Fragments directly. And not in the adapter but in the Activity itself?
In this case you could do something like this:
1.
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
{
tab.setTabListener(this);
Fragment f = Fragment.instantiate(mContext, clss.getName(), args);
mFragments.add(f);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
2.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(!item.getTitle().toString().equals("Active") || !item.getTitle().toString().equals("Jugend"))
{
Fragment f = mFragments.get(getSupportActionBar().getSelectedTab().getPosition());
((Spiele)f).FormatiereSpieleSeite(item.getTitle().toString());
}
return true;
}
If I understand you correctly all you want to do is change the adapter. Right?
The solution is easyer than i think.
These https://stackoverflow.com/a/7393477/1231245 have helped me to find the FragmentTag from the first Fragment in ViewPager.
I can easilie create and set the adapter in the onOptionsItemSelected method.
Here is it:
List<String> dateAndSchiri = new ArrayList<String>();
dateAndSchiri.add.....
List<String> heim = new ArrayList<String>();
heim.add.....
List<String> gast = new ArrayList<String>();
gast.add.....
SpieleAdapterFuerCustomList adapter = new SpieleAdapterFuerCustomList(getBaseContext(), dateAndSchiri, heim, gast);
Spiele articleFrag = (Spiele) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":0");
articleFrag.setListAdapter(adapter);
you can create a new adapter with different ArrayList's with different click on menu...

Fragment is not restoring its state in a viewpager when back is pressed and reopened

I am trying to keep a view pager inside a fragment. And the View pager itself contains 3 other fragment. This is the Root fragment which contain the view pager
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import com.android.browser1.UI.ComboViews;
public class MyFragment extends Fragment implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener, CombinedBookmarksCallbacks {
private TabHost mTabHost;
private ViewPager mViewPager;
public static final String EXTRA_COMBO_ARGS = "combo_args";
Bundle bundle;
Bundle extra;
public static final String EXTRA_INITIAL_VIEW = "initial_view";
public static BookmarkFragment bookmarkFragmentForPageA = null;
public static BookmarkFragment bookmarkFragmentForPageB = null;
Controller controller;
TabsAdapter mTabsAdapter;
FirstFragment first;
SecondFragment seconde;
ThirdFragment third;
public void setBundle(Bundle bundle) {
extra = bundle;// getActivity().getIntent().getExtras();
this.bundle = extra.getBundle(EXTRA_COMBO_ARGS);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mTabHost.setup();
mViewPager.setOffscreenPageLimit(2);
mTabsAdapter = new TabsAdapter(getActivity(), mTabHost, mViewPager);
new setAdapterTask().execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bookmark_view_pager, null);
mTabHost = (TabHost) view.findViewById(R.id.tabHost);
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
view.setBackgroundColor(Color.BLACK);
return view;
}
private class setAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
String bookmark = getResources().getString(R.string.tab_bookmarks);
String history = getResources().getString(R.string.tab_history);
String scrapmemo = getResources().getString(R.string.tab_snapshots);
mTabsAdapter.addTab(
mTabHost.newTabSpec(bookmark).setIndicator(bookmark), BrowserBookmarksPage.class, null);
mTabsAdapter.addTab(
mTabHost.newTabSpec(history).setIndicator(history), BrowserHistoryPage.class, null);
mTabsAdapter.addTab(
mTabHost.newTabSpec(scrapmemo).setIndicator(scrapmemo), BrowserSnapshotPage.class, null);
String svStr = extra.getString(EXTRA_INITIAL_VIEW, null);
ComboViews startingView = svStr != null ? ComboViews.valueOf(svStr)
: ComboViews.Bookmarks;
switch (startingView) {
case Bookmarks:
mTabHost.setCurrentTab(0);
mViewPager.setCurrentItem(0);
break;
case History:
mTabHost.setCurrentTab(1);
mViewPager.setCurrentItem(1);
break;
case ScrapMemo:
mTabHost.setCurrentTab(2);
mViewPager.setCurrentItem(2);
break;
}
}
}
public void onTabChanged(String tag) {
ComboViews startingView = tag != null ? ComboViews.valueOf(tag)
: ComboViews.Bookmarks;
switch (startingView) {
case First:
mTabHost.setCurrentTab(0);
break;
case Second:
mTabHost.setCurrentTab(1);
break;
case Third:
mTabHost.setCurrentTab(2);
break;
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
public class TabsAdapter extends FragmentStatePagerAdapter
implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
final class TabInfo {
private final String tag;
private final Class clss;
private final Bundle args;
TabInfo(String _tag, Class _class, Bundle _args) {
tag = _tag;
clss = _class;
args = _args;
}
}
class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
#Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(Activity activity, TabHost tabHost, ViewPager pager) {
super(activity.getFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
mViewPager.setOffscreenPageLimit(3);
}
public void addTab(TabHost.TabSpec tabSpec, Class clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
getItem(0);
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public Fragment getItem(int position) {
Fragment fr = null;
TabInfo info = mTabs.get(position);
// Create a new fragment if necessary.
switch (position) {
case 0:
fr = new FirstFragment();
fr.setArguments(info.args);
break;
case 1:
fr = new SecondFragment ();
fr.setArguments(info.args);
break;
case 2:
fr = new ThirdFragment ();
fr.setArguments(info.args);
break;
default:
fr = new FirstFragment ();
fr.setArguments(info.args);
break;
}
return fr;
}
#Override
public int getItemPosition(Object object) {
if (object instanceof FirstFragment
|| object instanceof SecondFragment
|| object instanceof ThirdFragment )
return POSITION_NONE;
return POSITION_UNCHANGED;
}
#Override
public void onTabChanged(String tabId) {
// called when the user clicks on a tab.
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Unfortunately when TabHost changes the current tab, it kindly
// also takes care of putting focus on it when not in touch mode.
// The jerk.
// This hack tries to prevent this from pulling focus out of our
// ViewPager.
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
}
And when I attach this fragment in my activity for the first time we can see all the three fragment by swiping but after the back press we remove the fragment. After that again I attach the fragment in the same activity at run time but only first 2 fragments are visible and the content of 3rd one is empty.
I am trying to keep a view pager inside a fragment. And the View pager itself contains 3 other fragment.
Sorry, but fragments inside of other fragments is officially not supported, as indicated by Dianne Hackborn, a core Android team member.

Categories

Resources