Tengo dos TabView
y el problema es que si por ejemplo tengo un Toast
en Tab2
al abrir Tab1
se ejecuta, cómo puedo hacer para que no "intercambien datos", es decir, si estoy en Tab1
o Tab2
que la otra no se active, que este "cerrada"
FragmentStatePagerAdapter
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Activity
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Mis Notas"));
tabLayout.addTab(tabLayout.newTab().setText("Favoritas"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
EDITO1: @Israel
public class Tab1 extends Fragment {
public static SqliteDatabase mDatabase;
public static RecyclerView recyclerview_notas;
public static ArrayList<Notas> allNotas;
public static TextView TxtPersistente, RecyclerVacio;
static public NotasAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1,
container, false);
RecyclerVacio = (TextView) view.findViewById(R.id.RecyclerVacio);
recyclerview_notas = (RecyclerView) view.findViewById(R.id.recyclerview_notas);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ContextyMetodos.getAppContext());
TxtPersistente = new TextView(ContextyMetodos.getAppContext());
recyclerview_notas.setLayoutManager(linearLayoutManager);
recyclerview_notas.setHasFixedSize(true);
//metodoMuestraRecyclerView();
return view;
}
public void metodoMuestraRecyclerView(){
mDatabase = new SqliteDatabase(ContextyMetodos.getAppContext());
allNotas = (ArrayList<Notas>) mDatabase.listNotas();
if (allNotas.size() > 0) {
RecyclerVacio.setVisibility(View.GONE);
recyclerview_notas.setVisibility(View.VISIBLE);
mAdapter = new NotasAdapter(ContextyMetodos.getAppContext(), allNotas);
recyclerview_notas.setAdapter(mAdapter);
mAdapter.filter("favorito");
} else {
recyclerview_notas.setVisibility(View.GONE);
RecyclerVacio.setVisibility(View.VISIBLE);
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
metodoMuestraRecyclerView();
}
}
}
Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at intento.dos.TabView.Tab1.metodoMuestraRecyclerView(Tab1.java:52)
at intento.dos.TabView.Tab1.setUserVisibleHint(Tab1.java:68)
at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:157)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1266)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18799)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
at android.view.View.measure(View.java:18799)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
viewPager.setOffscreenPageLimit(2);
donde el 2, es el numero de tabs, no recuerdo si era en base 0 o 1, pero para asegurar coloca 2Activity
diferente, es decir, que no se "junten" entre si, si agrego unToast
enTab2
también se ejecuta enTab1
, necesito que cada vez que este en unTab
los demás no se ejecuten, que cada vez que vaya de uno a otro, cargué de cero eseTab
. No sé si será posible, espero haberme explicado ahora bien, gracias @IsraelToast
, te refieres a que al lanzarlo en tab1 e ir a tab2 el toast aun es visible ?0
, a lo que me refiero es que por ejemplo tengo unToast
enTab2
, inicio laActivity
que inicia enTab1
y se muestra elToast
deTab2
. Al iniciar laActivity
se ejecuta el código de ambosTab
. Yo quiero que solamente se ejecute el código delTab
en el que estoy @Israel