【问题标题】:Reuse of same layouts inside one layout using multiple includes - TabLayout + ViewPager + RecyclerView (multiple viewpagers in one View)使用多个包含在一个布局内重用相同布局 - TabLayout + ViewPager + RecyclerView(一个视图中的多个视图页面)
【发布时间】:2016-12-01 14:27:08
【问题描述】:

我使用的是 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 上每个选项卡的片段( @+id/layout_horario_bnupom),连代码都一样。

【问题讨论】:

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


    【解决方案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 框架使用了 viewpager 的第一个实例(布局上的第一个 viewpager),而不是重新创建另一个。

    所以??????

    所以解决方案是为每个 viewpager/tablayout 提供特定的 id

    :)

    编辑: 好的,但是您是如何得出这个结论的? (框架,实例,bla bla bla?)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多