我正在使用Galaxy Tab E SM-T560。我是一名3年的android開發人員,只是在網絡搜索問題解決方面工作。我搜索了像通貨膨脹和getParent()視圖實現的東西,他們都沒有工作。在一個佈局內重複使用包含多個佈局的相同佈局 - TabLayout + ViewPager + RecyclerView(在一個視圖中的多個視圖)
在我來說,我對平板電腦的佈局,有2包括具有TabLayout加ViewPager佈局,並與該viewpager我recyclerview使用
我的代碼是不是在某些時候工作,我已經做了一些複製粘貼,知道爲什麼不工作,但沒有進行長時間的搜索。
下面是這裏面
http://i.stack.imgur.com/UZigU.png
這裏只有一個工作視角的圖像,是做所有的這種神奇的抽屜右側的片段代碼:
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);
}
這是我的佈局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>
這將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工作正常,爲手機所有的意見都很好,只是在這種情況下,horario_tablet.xml內的第二佈局的顯示選項卡,但不會顯示第二個佈局(@ + id/layout_horario_bnupom)的tablayout上的每個選項卡的片段,即使代碼相同。