I am using a Galaxy Tab E SM-T560. I am a 3 years android developer, just working on internet-search-problem-solving. I have searched for View implementations like inflation and getParent() things, and none of them worked.
In my case I have a layout for tablets, that has 2 includes of a Layout that has TabLayout plus ViewPager, and with this viewpager I use it with recyclerview
My code is not working at some point, I have made some copy paste to know why is not working, but didn't proceed for long searches.
Here is an image of only one working view inside this
http://i.stack.imgur.com/UZigU.png
Here is the code of the fragment that does all of this magic in the right of the drawer:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.adriankohls.volkmannbus2.R;
import br.com.adriankohls.volkmannbus2.fragmentstatepageadapters.horario.HorarioBlumenauPomerodeFragmentStatePageAdapter;
import br.com.adriankohls.volkmannbus2.fragmentstatepageadapters.horario.HorarioPomerodeBlumenauFragmentStatePageAdapter;
import br.com.adriankohls.volkmannbus2.utils.AppUtils;
public class HorarioFragment extends Fragment {
private boolean isToBlumenau;
private View rootView;
public static HorarioFragment newInstance(boolean isToBlumenau) {
Bundle bundle = new Bundle();
HorarioFragment fragment = new HorarioFragment();
fragment.isToBlumenau = isToBlumenau;
bundle.putString(fragment.getTag(), AppUtils.getFragmentKey(fragment));
fragment.setArguments(bundle);
return fragment;
}
public HorarioFragment() {
super();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (rootView == null) {
if (!AppUtils.isTablet()) {
rootView = inflater.inflate(R.layout.fragment_viewpager_tablayout_horario, container, false);
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
TextView tvDestino = (TextView) rootView.findViewById(R.id.tvDestino);
ImageView imgInfo = (ImageView) rootView.findViewById(R.id.imgInfo);
if (isToBlumenau) {
viewPager.setAdapter(new HorarioPomerodeBlumenauFragmentStatePageAdapter(getFragmentManager(), getContext()));
tvDestino.setText(getResources().getStringArray(R.array.rota_horario)[0]);
} else {
viewPager.setAdapter(new HorarioBlumenauPomerodeFragmentStatePageAdapter(getFragmentManager(), getContext()));
tvDestino.setText(getResources().getStringArray(R.array.rota_horario)[1]);
}
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
AppUtils.tabLayoutRuleTablet(tabLayout);
tvDestino.setOnClickListener(switchHorario());
imgInfo.setOnClickListener(infoHorario());
} else {
rootView = inflater.inflate(R.layout.horario_tablet, container, false);
rootView.post(new Runnable() {
@Override
public void run() {
setUpPomerodeBlumenau();
setUpBlumenauPomerode();
}
});
}
}
return rootView;
}
private void setUpPomerodeBlumenau() {
View view = rootView.findViewById(R.id.layout_horario_pombnu);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(
new HorarioPomerodeBlumenauFragmentStatePageAdapter(
getChildFragmentManager(),
getContext()));
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setUpBlumenauPomerode() {
View view = rootView.findViewById(R.id.layout_horario_bnupom);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(
new HorarioBlumenauPomerodeFragmentStatePageAdapter(
getChildFragmentManager(),
getContext()));
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
This is my layout horario_tablet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout_horario_pombnu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="10dp"
android:text="@string/pomerodeblumenau"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
<include layout="@layout/fragment_viewpager_tablayout" />
</LinearLayout>
<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#FFF" />
<LinearLayout
android:id="@+id/layout_horario_bnupom"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="10dp"
android:text="@string/blumenaupomerode"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
<include layout="@layout/fragment_viewpager_tablayout" />
</LinearLayout>
</LinearLayout>
And this the fragment_viewpager_tablayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
app:tabBackground="@color/colorPrimaryDark"
app:tabGravity="center"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@color/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
RecyclerView is working fine, for phones all of the views are fine, just on this case, the second layout inside the horario_tablet.xml show the tabs but won't show the fragments for each tab on tablayout of the second layout (@+id/layout_horario_bnupom), even the code is the same.
Found the answer I have replaced all the tags for the hardcoded XML inclusion before in horario_tablet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout_horario_pombnu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="10dp"
android:text="@string/pomerodeblumenau"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_pombnu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs_pombnu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
app:tabBackground="@color/colorPrimaryDark"
app:tabGravity="center"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@color/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_pombnu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#FFF" />
<LinearLayout
android:id="@+id/layout_horario_bnupom"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="10dp"
android:text="@string/blumenaupomerode"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_bnupom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs_bnupom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
app:tabBackground="@color/colorPrimaryDark"
app:tabGravity="center"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@color/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_bnupom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
</LinearLayout>
OK, but why did you this?
It's simple: for some reason Android Framework was using the first instance of the viewpager (first viewpager on layout) and not recreating another.
SO??????
So the solution was to give specific ids for each viewpager/tablayout
:)
EDIT: OK, but how did you reach that conclusion? (Framework, instance, bla bla bla?)
Found the explanation here: https://groups.google.com/forum/#!topic/android-developers/cKdvKyneHYY
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments