2017-07-11 14 views
0

タブ間をスワイプすることはできますが、多くのタブを保持しますが、アクションバーのすぐ下のタブバーはあまりにもぎこちないです。 My screenshot。だから私はタブバー自体にスワイプ機能を追加したい。それは非常に簡単ですTabLayoutでスワイプを追加する(タブバー)

public class MainActivity extends AppCompatActivity { 

private SectionsPagerAdapter mSectionsPagerAdapter; 

private ViewPager mViewPager; 

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

    //sets custom toolbar as actionbar 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    // Create the adapter that will return a fragment for each of the two primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // 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); 
} 



@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); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    public PlaceholderFragment() { 
    } 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     // Show 8 total pages. 
     return 8; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) {   //Gets the title of the tabs 
     switch (position) { 
      case 0: 
       return "SECTION 1"; 
      case 1: 
       return "SECTION 2"; 
      case 2: 
       return "SECTION 2"; 
      case 3: 
       return "SECTION 2"; 
      case 4: 
       return "SECTION 2"; 
      case 5: 
       return "SECTION 2"; 
      case 6: 
       return "SECTION 2"; 
      case 7: 
       return "SECTION 2"; 
      // case 1: 
      // return "SECTION 2"; 
     } 
     return null; 
    } 
} 
+0

こんにちは、[最小、完全、かつ検証可能な例](https://stackoverflow.com/help/mcve)を作成する方法と[よくある質問を表示する方法](https ://stackoverflow.com/help/how-to-ask)ので、フィードバックや役に立つ回答を得る機会が増えます。 – DarkCygnus

+0

私はその文書を読んだが、私は自分の言葉をはっきりと表現できないと思う。質問の編集を手伝ってください。 – Nautatva

答えて

0

、このことができます

app:tabMode="fixed" 

希望するのではなく、この属性

app:tabMode="scrollable" 

を入れて、あなたのtabLayoutで:Example

は、ここに私のコードです。

+0

Ohh。ありがとう。 @GrayCygnusによって指摘されているように、どのようにして正確に件名を表現できるか考えてみてください。 – Nautatva

+0

質問している質問のタイトルについて話しています。あなたが正しい答えを見つけたら、それを正しいものとしてマークしてください。 –

+0

私はそれを正しいと印を付けましたが、私の評判は投票として公然と示されていません。 – Nautatva

関連する問題