2017-09-24 8 views
0

tabLayoutに新しいタブを追加できません。tabLayoutのタブを追加できません

XMLファイル

<RelativeLayout 
android:id="@+id/main_layout" 
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:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.dailam103.messagetimer.MainActivity"> 
<android.support.design.widget.TabLayout 
    android:id="@+id/tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="fixed" 
    app:tabGravity="fill"/> 

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</android.support.v4.view.ViewPager> 
</RelativeLayout> 

MainActivity.java

package com.dailam103.messagetimer; 
import android.os.Bundle; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.support.design.widget.TabLayout; 
public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.addTab(tabLayout.newTab().setTag(1).setText("Tab 1")); 
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final TabAdapter adapter = new TabAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){ 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 
     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

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

} 
} 

エラーが

tabLayout.addTab(tabLayout.newTab()である。setTag(1).setText(「タブ1 ")));

tabLayoutにタブを追加できない理由を教えてください。

+0

を使用logcatではなく、そのダイアログの画像を投稿 –

答えて

1

このように長くコードする必要はありません。

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){ 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 
     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

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

ただ、どこからでもtablayoutを設定するために有用であろうtabLayout.setupWithViewPager(viewPager);

  • またはコードを追加します。

    public class ViewPagerBean { 
    ViewPager viewPager; 
    TabLayout tabLayout; 
    ArrayList<TabBean> tabTitleList; 
    
    
    public ViewPager getViewPager() { 
        return viewPager; 
    } 
    
    public void setViewPager(ViewPager viewPager) { 
        this.viewPager = viewPager; 
    } 
    
    public TabLayout getTabLayout() { 
        return tabLayout; 
    } 
    
    public void setTabLayout(TabLayout tabLayout) { 
        this.tabLayout = tabLayout; 
    } 
    
    public ArrayList<TabBean> getTabTitleList() { 
        return tabTitleList; 
    } 
    
    public void setTabTitleList(ArrayList<TabBean> tabTitleList) { 
        this.tabTitleList = tabTitleList; 
    } 
    } 
    
    public class TabBean { 
    int icon; 
    int title; 
    BaseFragment baseFragment; 
    
    public TabBean(int icon, int title, BaseFragment baseFragment) { 
    this.icon = icon; 
    this.title = title; 
    this.baseFragment = baseFragment; 
    } 
    
    public int getIcon() { 
    return icon; 
    } 
    
    public void setIcon(int icon) { 
    this.icon = icon; 
    } 
    
    public int getTitle() { 
    return title; 
    } 
    
    public void setTitle(int title) { 
    this.title = title; 
    } 
    
    public BaseFragment getBaseFragment() { 
    return baseFragment; 
    } 
    
    public void setBaseFragment(BaseFragment baseFragment) { 
    this.baseFragment = baseFragment; 
    } 
    } 
    
  • 私のすべてのフラグメントはBaseFragmentになります。あなたはBaseFragmentをあなたのBaseActivityクラスに置き換えて、活動中のtablayoutが必要な場合にだけ置き換えてください。

    private TabLayout tabLayout; 
    private ViewPager viewPagerImage; 
    
    tabLayout = (TabLayout) fragmentView.findViewById(R.id.tabLayoutDefault); 
    viewPagerImage = (ViewPager) fragmentView.findViewById(R.id.homeViewPager); 
    
  • このコードをBaseFragmentまたはBaseActivityクラスに書き込みます。

    public void setViewPager(ViewPagerBean viewPagerBean) { 
    ViewPager viewPager = viewPagerBean.getViewPager(); 
    TabLayout tabLayout = viewPagerBean.getTabLayout(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    PagerAdapter adapterMyEvents = new PagerAdapter(fragmentManager, 
    viewPagerBean.getTabTitleList()); 
    viewPager.setAdapter(adapterMyEvents); 
    tabLayout.setupWithViewPager(viewPager); 
    viewPager.setCurrentItem(0); 
    setCustomTabView(viewPagerBean); 
    } 
    
    private void setCustomTabView(ViewPagerBean pagerBean) { 
    for (int i = 0; i < pagerBean.getTabLayout().getTabCount(); i++) { 
    TabLayout.Tab tab = pagerBean.getTabLayout().getTabAt(i); 
    LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.tab_custom_layout, pagerBean.getTabLayout(), false); 
    TextView tabTextView = (TextView) linearLayout.findViewById(R.id.tab_title); 
    ImageView ivTabLeftIcon = (ImageView) linearLayout.findViewById(R.id.ivTabLeftIcon); 
    TabBean tabBean = pagerBean.getTabTitleList().get(i); 
    
    if (tabBean.getTitle() != 0) 
        tabTextView.setText(tabBean.getTitle()); 
    if (tabBean.getIcon() != 0) 
        ivTabLeftIcon.setImageResource(tabBean.getIcon()); 
    if (tab != null) { 
        tab.setCustomView(linearLayout); 
    } 
    } 
    } 
    
    
    
    
    private class PagerAdapter extends FragmentStatePagerAdapter { 
    
    private ArrayList<TabBean> arrayList; 
    
    public PagerAdapter(FragmentManager fm, ArrayList<TabBean> mTitles) { 
    super(fm); 
    this.arrayList = mTitles; 
    } 
    
    @Override 
    public Fragment getItem(int position) { 
    return arrayList.get(position).getBaseFragment(); 
    } 
    
    
    @Override 
    public CharSequence getPageTitle(int position) { 
    return getString(arrayList.get(position).getTitle()); 
    } 
    
    
    @Override 
    public int getCount() { 
    return arrayList.size(); 
    } 
    } 
    
  • と簡単にこのよう

     public ArrayList<TabBean> getUserHomeTabFragmentList() { 
         ArrayList<TabBean> arrayList = new ArrayList<>(); 
    
         FragmentHomeLunchList fragmentMenuLunch = new FragmentHomeLunchList(); 
         FragmentHomeDinnerList fragmentMenuDinner = new 
         FragmentHomeDinnerList(); 
    
         arrayList.add(new TabBean(R.drawable.ic_lunch, R.string.lunch, fragmentMenuLunch)); 
         arrayList.add(new TabBean(R.drawable.ic_dinner, R.string.dinner, fragmentMenuDinner)); 
    
         return arrayList; 
         } 
    
         private void setPagerAdapterWithTabLayout() { 
         ArrayList<TabBean> tabsList = UtilProject.getInstance().getList(); 
         ViewPagerBean viewPagerBean = new ViewPagerBean(); 
         viewPagerBean.setTabLayout(tabLayout); 
         viewPagerBean.setTabTitleList(tabsList); 
         viewPagerBean.setViewPager(viewPagerBottom); 
         setViewPager(viewPagerBean); 
         viewPagerBottom.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
         @Override 
         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
    
         } 
    
         @Override 
         public void onPageSelected(int position) { 
    
         } 
    
         @Override 
         public void onPageScrollStateChanged(int state) { 
    
         } 
         }); 
         } 
    
関連する問題