2015-11-17 63 views
0

我的Android應用程序中有一個Activity,其中包含TabLayout以在不同片段之間切換。我實現了一個自定義的FragmentStatePagerAdapter來插入我的列,並使用tabLayout.setupWithViewPager(mViewPager)方法在我的Activity中初始化它。將TabLayout的元素嵌入屏幕

我的問題是,如果我用我的TabLayout.MODE_FIXED我tabLayout結束與元素,即討論的一個範圍內,如扭曲,如下圖:

tabLayout with TabLayout.MODE_FIXED

而且在其他情況下,如果我用TabLayout.MODE_SCROLLABLE,我結束了我所有的tabLayout的預定義的元素作爲收縮與大量的空的空間應用的左上角無論哪個重力我指定爲這樣:

tabLayout with TabLayout.MODE_SCROLLABLE

我試圖獲得的結果是:

1)固定模式下的TabLayout沒有兩個班輪或重疊的元素,或;

2)可滾動模式下的TabLayout至少覆蓋所有可用空間和/或覆蓋超過給定空間,因爲只要用戶可以在TabLayout中滾動以查看和訪問不可見標籤,它將被接受。

相關來源和資源代碼如下。如果需要更多信息,請告訴我。

我的活動代碼:

import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import swe574.boun.edu.androidproject.adapters.GroupTabPagerAdapter; 

public class GroupActivity extends AppCompatActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    private GroupTabPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 
    private List<String> mTitles; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_group); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mTitles = new ArrayList<>(); 
     mTitles.addAll(Arrays.asList(new String[]{"Home" , "Meetings" , "Discussions" , "Notes"})); 
     mSectionsPagerAdapter = new GroupTabPagerAdapter(getSupportFragmentManager(), mTitles, 4); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 
     tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_group, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

我GroupTabPagerAdapter代碼:

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 

import java.util.List; 

import swe574.boun.edu.androidproject.fragments.DiscussionFragment; 
import swe574.boun.edu.androidproject.fragments.GroupHomeFragment; 
import swe574.boun.edu.androidproject.fragments.MeetingFragment; 
import swe574.boun.edu.androidproject.fragments.NoteFragment; 

public class GroupTabPagerAdapter extends FragmentStatePagerAdapter { 
    private List<String> mTitles; 
    private int mNumOfTabs; 

    public GroupTabPagerAdapter(FragmentManager fm, List<String> mTitles, int mNumOfTabs) { 
     super(fm); 
     this.mTitles = mTitles; 
     this.mNumOfTabs = mNumOfTabs; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = null; 
     switch (position) { 
      case 0: 
       fragment = new GroupHomeFragment(); 
       break; 
      case 1: 
       fragment = new MeetingFragment(); 
       break; 
      case 2: 
       fragment = new DiscussionFragment(); 
       break; 
      case 3: 
       fragment = new NoteFragment(); 
       break; 
     } 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return mNumOfTabs; 
    } 

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


} 

我的動態佈局:

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="swe574.boun.edu.androidproject.GroupActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

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

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

感謝您的幫助。

回答

1

我解決這個問題,在自定義類擴展TabLayout以及與此重寫onMeasure方法:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    try { 
     if (getTabCount() == 0){ 
      return; 
     } 
     Field field = TabLayout.class.getDeclaredField("mTabMinWidth"); 
     field.setAccessible(true); 
     field.set(this, (int) (getMeasuredWidth()/(float) getTabCount()) * 1); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}