2016-07-27 91 views
0

我正在使用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上的每個選項卡的片段,即使代碼相同。

回答

1

發現我已經取代所有的硬編碼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

相關問題