使用多个包含在一个布局内重用相同的布局 - TabLayout + ViewPager + RecyclerView(一个视图中的多个viewpage)

时间:2016-07-27 14:07:08

标签: android xml android-layout android-fragments layout

我使用的是Galaxy Tab E SM-T560。我是一名3年的Android开发人员,正致力于解决网络搜索问题。我搜索了像通胀和 getParent()这样的View实现,但没有一个能够工作。

在我的情况下,我有一个平板电脑的布局,其中包含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内的第二个布局显示了标签,但是没有显示第二个tablayout上的每个标签的片段layout(@ + id / layout_horario_bnupom),即使代码也一样。

1 个答案:

答案 0 :(得分:1)

找到了这件衣服 在horario_tablet.xml

之前,我已经替换了硬编码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>

好的,但你为什么这么做?

这很简单:出于某种原因,Android Framework使用了viewpager的第一个实例(布局上的第一个viewpager)而没有重新创建另一个实例。

<强> SO ??????

因此解决方案是为每个viewpager / tablayout提供特定的ID

<强> :)

修改 好的,但你是怎么得出这个结论的? (框架,实例,bla bla bla?)

在此处找到解释:https://groups.google.com/forum/#!topic/android-developers/cKdvKyneHYY