在一个布局内重复使用包含多个布局的相同布局 - TabLayout + ViewPager + RecyclerView(在一个视图中的多个视图)
我正在使用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上的每个选项卡的片段,即使代码相同。
发现我已经取代所有的硬编码XML列入标签之前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了anwear ,但你为什么要这样?
很简单:出于某种原因,Android Framework使用viewpager的第一个实例(布局上的第一个viewpager),而不是重新创建另一个。
SO ??????
因此,解决办法是给特定ID对每个viewpager/tablayout
:)
编辑: OK,但你怎么得出那个结论的? (框架,例如,唧唧歪歪?)
找到解释这里:https://groups.google.com/forum/#!topic/android-developers/cKdvKyneHYY