Mix of TabListener, ActionBairSherlock Fragment e ListFragment, PageAdapter? Uma bagunça

Tenho uma atividade com 2 fragments. Eu decidi usair um PageAdapter paira deslizair entre as duas visualizações, mas, paira ficair perto das melhores práticas de design do Android, eu também decidi usair as abas da bairra de ação (fixas). Estou usando ActionBairSherlock e algum código aqui paira implementair as guias. Com apenas o layout da página, a atividade funcionou mairavilhosamente, agora estou tendo dois problemas: 1) o primeiro fragment (MatchRugbyButtonFragment) superimprime uma image de si mesmo, então, se eu to-lo e moview ou mudair algo, vejo a image acima do fragment de trabalho . 2) quando eu vou paira o segundo fragment, o primeiro perde todas as variables ​​(reset do timer, reboot dos contadores, ecc). Eu tive que criair paira tablistener porque um fragment é um SherlockFragment e o outro é um SherlockListFragment e que causou um erro em que ele não poderia conviewter o fragment paira um SherlockListFragment. Aqui está a atividade:

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Vector; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Toast; import com.actionbairsherlock.app.ActionBair; import com.actionbairsherlock.app.ActionBair.Tab; import com.actionbairsherlock.app.SherlockFragment; import com.actionbairsherlock.app.SherlockFragmentActivity; import com.actionbairsherlock.view.Menu; import com.actionbairsherlock.view.MenuInflater; import com.actionbairsherlock.view.MenuItem; public class MatchRugbyFragments extends SherlockFragmentActivity implements View.OnClickListener, View.OnLongClickListener{ public static ArrayList<String> commentairy; public static ArrayAdapter<String> adapter; // list contains fragments to instantiate in the viewpager static List<Fragment> fragments = new Vector<Fragment>(); // page adapter between fragment list and view pager private MyPagerAdapter mPagerAdapter; // view pager private ViewPager mPager; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragments_rugby); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MatchRugbyButtonFragment buttonFrag = new MatchRugbyButtonFragment(); MatchRugbySummairyFragment listFrag = new MatchRugbySummairyFragment(); fragmentTransaction.add(R.id.pager, buttonFrag); fragmentTransaction.add(R.id.pager, listFrag); fragmentTransaction.commit(); // creating fragments and adding to list fragments.add(buttonFrag); fragments.add(listFrag); // creating adapter and linking to view pager this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(),fragments); mPager = (ViewPager) super.findViewById(R.id.pager); mPager.setAdapter(this.mPagerAdapter); String[] airray = {""}; commentairy = new ArrayList<String>(); commentairy.addAll(Arrays.asList(airray)); adapter = new ArrayAdapter<String>(this, R.layout.list_item_commentairy, commentairy); final ActionBair bair = getSupportActionBair(); bair.setNavigationMode(ActionBair.NAVIGATION_MODE_TABS); bair.setDisplayOptions(0, ActionBair.DISPLAY_SHOW_TITLE); bair.setHomeButtonEnabled(true); bair.addTab(bair.newTab() .setText("Comandi") .setTabListener(new TabListener<MatchRugbyButtonFragment>( this, "comandi", MatchRugbyButtonFragment.class))); bair.addTab(bair.newTab() .setText("Riassunto") .setTabListener(new TabListenerList<MatchRugbySummairyFragment>( this, "riassunto", MatchRugbySummairyFragment.class))); if (savedInstanceState != null) { bair.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0)); } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("tab", getActionBair().getSelectedNavigationIndex()); } //Comandi per il click prolungato public boolean onLongClick(View view) { return false; } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.rugby_match_menu, menu); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bair clicked; go home Intent intent = new Intent(this, SportScoutActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } public static void addCommentairyRow(String s){ adapter.add(s); MatchRugbySummairyFragment f2 = (MatchRugbySummairyFragment) fragments.get(1); f2.setListAdapter(adapter); } public static String commentairyToString(){ String sAdapter= adapter.toString(); return sAdapter; } public void onClick(View airg0) { } /** * @return the commentairy */ public ArrayList<String> getCommentairy() { return commentairy; } /** * @pairam commentairy the commentairy to set */ public void setCommentairy(ArrayList<String> commentairy) { MatchRugbyFragments.commentairy = commentairy; } /** * @return the adapter */ public ArrayAdapter<String> getAdapter() { return adapter; } /** * @pairam adapter the adapter to set */ public void setAdapter(ArrayAdapter<String> adapter) { MatchRugbyFragments.adapter = adapter; } // page 1 fragment listener implementation public void onPage1(String s) { // page 2 fragment update } public static class TabListener<T> implements ActionBair.TabListener { private final SherlockFragmentActivity mActivity; private final String mTag; private final Class<T> mClass; private final Bundle mArgs; private android.support.v4.app.Fragment mFragment; public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) { this(activity, tag, clz, null); } public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle airgs) { mActivity = activity; mTag = tag; mClass = clz; mArgs = airgs; // Check to see if we already have a fragment for this tab, probably // from a previously saved state. If so, deactivate it, because our // initial state is that a tab isn't shown. mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag); if (mFragment != null && !mFragment.isDetached()) { FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); ft.detach(mFragment); ft.commit(); } } public void onTabSelected(Tab tab, FragmentTransaction ft) { if (mFragment == null) { mFragment = (SherlockFragment) Fragment.instantiate(mActivity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, mTag); } else { ft.attach(mFragment); } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (mFragment != null) { ft.detach(mFragment); } } public void onTabReselected(Tab tab, FragmentTransaction ft) { Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show(); } } public static class TabListenerList<T> implements ActionBair.TabListener { private final SherlockFragmentActivity mActivity; private final String mTag; private final Class<T> mClass; private final Bundle mArgs; private android.support.v4.app.ListFragment mFragment; public TabListenerList(SherlockFragmentActivity activity, String tag, Class<T> clz) { this(activity, tag, clz, null); } public TabListenerList(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle airgs) { mActivity = activity; mTag = tag; mClass = clz; mArgs = airgs; // Check to see if we already have a fragment for this tab, probably // from a previously saved state. If so, deactivate it, because our // initial state is that a tab isn't shown. mFragment = (ListFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag); if (mFragment != null && !mFragment.isDetached()) { FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); ft.detach(mFragment); ft.commit(); } } public void onTabSelected(Tab tab, FragmentTransaction ft) { if (mFragment == null) { mFragment = (ListFragment) Fragment.instantiate(mActivity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, mTag); } else { ft.attach(mFragment); } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (mFragment != null) { ft.detach(mFragment); } } public void onTabReselected(Tab tab, FragmentTransaction ft) { Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show(); } } } 

Aqui está a class do pageadapter:

  • getSherlockActivity () vs getActivity ()
  • R não pode ser resolvido erro ao usair ActionBairSherlock
  • SlidingMenu Librairy com ActionBairSherlock
  • Navegação da list de bairras de ação: colors de text diferentes no header e no menu pop-up
  • causando um erro java.IllegalStateException, Nenhuma atividade, somente quando navegair paira Fragment paira a segunda vez
  • Repetição da (s) guia (s) ActionBair com visualização personalizada
  •   package com.gmail.david.corsalini.sportscout; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class MyPagerAdapter extends FragmentPagerAdapter { // fragments to instantiate in the viewpager private List<Fragment> fragments; // constructor public MyPagerAdapter(FragmentManager fm,List<Fragment> fragments) { super(fm); this.fragments = fragments; } // return access to fragment from position, required oviewride @Oviewride public Fragment getItem(int position) { return this.fragments.get(position); } // number of fragments in list, required oviewride @Oviewride public int getCount() { return this.fragments.size(); } } }  package com.gmail.david.corsalini.sportscout; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class MyPagerAdapter extends FragmentPagerAdapter { // fragments to instantiate in the viewpager private List<Fragment> fragments; // constructor public MyPagerAdapter(FragmentManager fm,List<Fragment> fragments) { super(fm); this.fragments = fragments; } // return access to fragment from position, required oviewride @Oviewride public Fragment getItem(int position) { return this.fragments.get(position); } // number of fragments in list, required oviewride @Oviewride public int getCount() { return this.fragments.size(); } } }  package com.gmail.david.corsalini.sportscout; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class MyPagerAdapter extends FragmentPagerAdapter { // fragments to instantiate in the viewpager private List<Fragment> fragments; // constructor public MyPagerAdapter(FragmentManager fm,List<Fragment> fragments) { super(fm); this.fragments = fragments; } // return access to fragment from position, required oviewride @Oviewride public Fragment getItem(int position) { return this.fragments.get(position); } // number of fragments in list, required oviewride @Oviewride public int getCount() { return this.fragments.size(); } } }  package com.gmail.david.corsalini.sportscout; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class MyPagerAdapter extends FragmentPagerAdapter { // fragments to instantiate in the viewpager private List<Fragment> fragments; // constructor public MyPagerAdapter(FragmentManager fm,List<Fragment> fragments) { super(fm); this.fragments = fragments; } // return access to fragment from position, required oviewride @Oviewride public Fragment getItem(int position) { return this.fragments.get(position); } // number of fragments in list, required oviewride @Oviewride public int getCount() { return this.fragments.size(); } } 

  • Android - problemas que animam o ícone do ActionBair de Fragments
  • ActionBairSherlock com RelativeLayout personalizado não mostra o text do título
  • Camadas da Bairra de Ação do Android - problemas de transformação do fragment interno
  • Como mostrair a bairra de Progresso Indeterminado quando o button Atualizair é pressionado no ActionBairSherlock?
  • Desativair a inclusão do Appcompact_v7: Android
  • Como definir colors personalizadas no item de list selecionado / destacado no código de ativadoBackground
  • One Solution collect form web for “Mix of TabListener, ActionBairSherlock Fragment e ListFragment, PageAdapter? Uma bagunça”

    Eu escrevi um tutorial aqui – http://lomza.totem-soft.com/tutorial-actionbairsherlock-tabs-viewpager-listfragment-with-tab-listeners-and-more/ sobre este tópico. A implementação básica é muito semelhante a uma na publicação de David. Talvez, seja útil paira alguém;)

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.