2015-07-28 40 views
1

我在加載每個製表符的子片段的片段中使用ViewPager,並且該子片段加載的工作正常,但問題在於製表符本身,不能顯示標籤可點擊的名稱,也不會響應點擊事件。我通過在前一個片段的回收站中選擇一個元素來達到此屏幕。Android ViewPager子片段加載但製表符不要

預計的佈局結構如下: Expected Layout

但問題是,雖然一切加載罰款,我可以向左滑動切換子片段,標籤欄完全不和我加載剛剛得到這個:

Current Flawed layout

我不知道是什麼原因造成這一點,但是當我旋轉屏幕和背部,然後他們出現。

MainActivity類別

public class MainActivity extends AppCompatActivity { 

private final static int PROFILE_FRAGMENT = 1; 
private final static int PRIZES_FRAGMENT = 2; 
private final static int STORES_FRAGMENT = 3; 
private final static int ABOUT = 4; 
private int currentFragment = 3; 
private String currentFragmentName = "StoresListFragment"; 
public int HEADER_IMAGE = R.drawable.avatar; 

private DrawerLayout drawer; 
private List<DrawerItem> dataList; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
    FragmentManager fm = getSupportFragmentManager(); 

    if (getIntent().getStringExtra("STORE_DETAILS") != null) { 
     openFragment(new StoreDetailsFragment()); 
     setTitle(getString(R.string.title_store_details)); 
    } 
    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    if (mToolbar != null) { 
     setSupportActionBar(mToolbar); 
    } 
    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); 
    mRecyclerView.setHasFixedSize(true); 
    dataList = new ArrayList<>(); 
    addItemsToDataList(); 
    NavDrawerAdapter mAdapter = new NavDrawerAdapter(dataList, this, db.getClientName(), db.getPoints(), HEADER_IMAGE); 
    mRecyclerView.setAdapter(mAdapter); 
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setLayoutManager(mLayoutManager); 
    drawer = (DrawerLayout) findViewById(R.id.DrawerLayout); 
    drawer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
      this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { 
     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 
    }; 
    drawer.setDrawerListener(mDrawerToggle); 
    mDrawerToggle.syncState(); 
    onTouchDrawer(currentFragment); 
    final GestureDetector mGestureDetector = 
      new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() { 
       @Override 
       public boolean onSingleTapUp(MotionEvent e) { 
        return true; 
       } 
      }); 
    mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { 
     @Override 
     public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { 
      View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); 

      if (child != null && mGestureDetector.onTouchEvent(motionEvent)) { 
       drawer.closeDrawers(); 
       onTouchDrawer(recyclerView.getChildLayoutPosition(child)); 
       return true; 
      } 
      return false; 
     } 

     @Override 
     public void onTouchEvent(RecyclerView rv, MotionEvent e) { 

     } 

     @Override 
     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

     } 
    }); 
} 

private void addItemsToDataList() { 
    dataList.add(new DrawerItem(getString(R.string.title_profile), R.mipmap.ic_action_profile)); 
    dataList.add(new DrawerItem(getString(R.string.title_prizes), R.mipmap.ic_action_prizes)); 
    dataList.add(new DrawerItem(getString(R.string.title_stores), R.mipmap.ic_action_sales)); 
    dataList.add(new DrawerItem(getString(R.string.title_about), R.mipmap.ic_action_about)); 
} 

public void openFragment(final Fragment fragment) { 
    if (!fragment.toString().equalsIgnoreCase(currentFragmentName)) { 
     // update the transfer content by replacing fragments 
     currentFragmentName = fragment.toString(); 
     switchContent(fragment); 
    } 
    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commitAllowingStateLoss(); 
} 

private void onTouchDrawer(final int position) { 
    currentFragment = position; 
    switch (position) { 
     case PROFILE_FRAGMENT: 
      openFragment(new ProfileFragment()); 
      setTitle(getString(R.string.title_profile)); 
      break; 
     case PRIZES_FRAGMENT: 
      openFragment(new PrizesListFragment()); 
      setTitle(getString(R.string.title_prizes)); 
      break; 
     case STORES_FRAGMENT: 
      openFragment(new StoresListFragment()); 
      setTitle(getString(R.string.title_stores)); 
      break; 
     case ABOUT: 
      openFragment(new AboutFragment()); 
      setTitle(getString(R.string.title_about)); 
      break; 
     default: 
    } 
} 

/* 
* helper to switch content with backstack 
*/ 
public void switchContent(Fragment fragment) { 

    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.container, fragment) 
        // add to backstack 
      .addToBackStack(fragment.getClass().getSimpleName()) 
      .commitAllowingStateLoss(); 
} 


@Override 
protected void onResume() { 
    if (getIntent().getStringExtra("SALES_LIST") != null) { 
     openFragment(new StoreDetailsFragment()); 
     setTitle(getString(R.string.title_store_details)); 

    } else if (getSupportFragmentManager().getBackStackEntryCount() <= 0) { 
     openFragment(new StoresListFragment()); 
     setTitle(getString(R.string.title_stores)); 
    } 
    super.onResume(); 
} 

}

有問題的片段:

片段

public class StoreDetailsFragment extends Fragment { 

private ViewPager viewPager; 
private Store storeSelected = null; 
private TextView storeName; 
private ImageView bannerImage; 
private TextView storesubtitle; 

public StoreDetailsFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_store_details, container, false); 

    viewPager = (ViewPager) rootView.findViewById(R.id.tabanim_viewpager); 
    TabLayout storeTabLayout = (TabLayout) rootView.findViewById(R.id.tabanim_tabs); 
    setupViewPager(viewPager); 

    storeTabLayout.setupWithViewPager(viewPager); 
    storeTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 

      viewPager.setCurrentItem(tab.getPosition()); 
      switch (tab.getPosition()) { 
       case 0: 
        Toast.makeText(getActivity(), "Sales List", Toast.LENGTH_SHORT).show(); 
        break; 
       case 1: 
        Toast.makeText(getActivity(), "Store Description", Toast.LENGTH_SHORT).show(); 
        break; 
       case 2: 
        Toast.makeText(getActivity(), "Store Ratings", Toast.LENGTH_SHORT).show(); 
        break; 
       case 3: 
        Toast.makeText(getActivity(), "Images", Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 
    bannerImage = (ImageView) rootView.findViewById(R.id.store_details_banner_image); 
    storeName = (TextView) rootView.findViewById(R.id.text_store_details_name); 
    storesubtitle = (TextView) rootView.findViewById(R.id.text_store_details_subtitle); 
    return rootView; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    // Set title 
    getMainActivity().setTitle(R.string.title_store_details); 
} 

protected MainActivity getMainActivity() { 
    return (MainActivity) getActivity(); 
} 

}有問題的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    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="300dp" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/store_details_banner_image" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="?attr/actionBarSize" 
       android:background="@drawable/white_placeholder" 
       android:contentDescription="" 
       android:fitsSystemWindows="true"/> 

      <LinearLayout 
       android:id="@+id/smth" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:layout_marginBottom="?attr/actionBarSize" 
       android:background="#ffffff" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/text_store_details_name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginStart="18dp" 
        android:layout_marginTop="40dp" 
        android:textColor="#333" 
        android:textSize="15sp" 
        android:textStyle="bold"/> 

       <TextView 
        android:id="@+id/text_store_details_subtitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="5dp" 
        android:layout_marginStart="13dp" 
        android:layout_marginTop="3dp" 
        android:drawableLeft="@mipmap/ic_action_store_house" 
        android:gravity="center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textColor="#333"/> 
      </LinearLayout> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/anim_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="@color/transparent" 
       app:layout_collapseMode="parallax" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabanim_tabs" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:layout_gravity="bottom" 
       android:visibility="visible" 
       app:tabGravity="center" 
       app:tabIndicatorColor="#F1514A" 
       app:tabMode="scrollable" 
       app:tabSelectedTextColor="@android:color/white" 
       app:tabTextColor="#99ffffff"/> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/tabanim_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

</android.support.design.widget.CoordinatorLayout> 

而且ViewPagerAdaper:

public class StoreDetailsViewPagerTabAdaper extends FragmentPagerAdapter { 
private final List<Fragment> mFragmentList = new ArrayList<>(); 
private final List<String> mFragmentTitleList = new ArrayList<>(); 

public StoreDetailsViewPagerTabAdaper(FragmentManager manager) { 
    super(manager); 
} 

@Override 
public android.support.v4.app.Fragment getItem(int position) { 

    return mFragmentList.get(position); 
} 

@Override 
public int getCount() { 
    return mFragmentList.size(); 
} 

public void addFrag(android.support.v4.app.Fragment fragment, String title) { 
    mFragmentList.add(fragment); 
    mFragmentTitleList.add(title); 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    return mFragmentTitleList.get(position); 
}} 

任何幫助和建議表示讚賞。

回答

0

最後,我找不到解決方案,甚至在諮詢過一些更有經驗的開發人員之後,我們只能找到一種解決方法,它只是在活動內部而不是片段,然後viewpager的行爲如同預期。我無法弄清楚爲什麼它不在片段內。 如果有人找到更合適的答案,請繼續,但在此之前,我會將其設置爲接受的答案。

0

我會嘗試登錄內部getPageTitle,因爲我覺得,當你旋轉屏幕的一切都毀壞了,當你重繪的標題,所以試圖找出發生了什麼事情做一樣的東西的mFragmentTitleList是空的:

@Override 
    public CharSequence getPageTitle(int position) { 

    Log.d("ViewPagerAdapter", "Displaying title: "+ mFragmentTitleList.get(position)); 
    return mFragmentTitleList.get(position); 
    } 
+0

我測試了它啓動你的適配器和正確的標題在日誌中顯示,這兩個的時候都不會顯示在屏幕上,當我旋轉,他們重新出現,所以信息就在那裏,只是因爲某種原因沒有顯示。 –

0

您的TabLayout可能未顯示,因爲您已將高度設置爲該值。嘗試設置android:layout_height="wrap_content"

你不必處理標籤手動點擊 - 如果你綁定TabLayoutViewPager

tabLayout.setupWithViewPager(viewPager); 

監聽器自動創建。我會建議刪除自定義偵聽器。

+0

我將tablayout高度設置爲與工具欄相同,以便它填充工具欄空間並且不會被擠壓,但是我使用wrap _content進行了測試,問題依然存在。另外,我只是設置了這個監聽器,這樣我就可以測試他的標籤在側滑動時是否正確切換,因爲我無法單擊這些標籤,但是您是正確的,我應該使用自動創建的標籤。 –

1
tabLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      tabLayout.setupWithViewPager(viewPager); 
     } 
    }); 

試試這一個,並與getChildFragmentManager

+0

它解決了我的問題... – AndroidLad

+0

@ohm更新你的庫,大多數23.0.0的bug是固定的 – Mike