2015-05-29 53 views
-2

我正在使用Florent37-MateriralViewPager庫它運行良好,但我的要求是標籤上的圖標。現在我只使用文本。 圖書館的MainActivity.java如下。我如何添加圖標到標籤使用「florent37/MaterialViewPager」

mViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager); 
    mViewPager.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(MainActivity.this, "T", Toast.LENGTH_LONG).show(); 
     } 
    }); 
    toolbar = mViewPager.getToolbar(); 
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

    if (toolbar != null) { 
     setSupportActionBar(toolbar); 

     final ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.setDisplayHomeAsUpEnabled(true); 
      actionBar.setDisplayShowHomeEnabled(true); 
      actionBar.setDisplayShowTitleEnabled(true); 
      actionBar.setDisplayUseLogoEnabled(true); 
      actionBar.setHomeButtonEnabled(true); 
     } 
    } 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, 0, 0); 
    mDrawer.setDrawerListener(mDrawerToggle); 


    mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { 

     int oldPosition = -1; 

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        return HotOffers.newInstance(); 
       case 1: 
        return TwentyOffers.newInstance(); 
       case 2: 
        return Brands.newInstance(); 
       case 3: 
        return Category.newInstance(); 
       default: 
        return RecyclerViewFragment.newInstance(); 
      } 
     } 

     @Override 
     public void setPrimaryItem(ViewGroup container, int position, Object object) { 
      super.setPrimaryItem(container, position, object); 

      //only if position changed 
      if (position == oldPosition) 
       return; 
      oldPosition = position; 

      int color = 0; 
      String imageUrl = "http://myimage.jpg"; 
      switch (position) { 
       case 0: 
        //imageUrl = arrayList.get(position); 
        color = getResources().getColor(R.color.cyan); 
        break; 
       case 1: 
        //imageUrl = arrayList.get(position); 
        color = getResources().getColor(R.color.cyan); 
        break; 
       case 2: 
        //imageUrl = arrayList.get(position); 
        color = getResources().getColor(R.color.cyan); 
        break; 
       case 3: 
        //imageUrl = arrayList.get(position); 
        color = getResources().getColor(R.color.cyan); 
        break; 
      } 

      final int fadeDuration = 500; 
      mViewPager.setImageUrl(imageUrl, fadeDuration); 
      mViewPager.setColor(color, fadeDuration); 

     } 

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "Top Twenty"; 
       case 1: 
        return "Just For You"; 
       case 2: 
        return "Brands"; 
       case 3: 
        return "Category"; 

      } 
      return ""; 
     } 
    }); 
    mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount()); 
    mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager()); 

    mViewPager.getViewPager().setCurrentItem(0); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 
    switch (id) { 


     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); 
} 

請幫助我如何將圖標添加到MaterialViewPager選項卡欄。謝謝。 enter image description here。想要添加這樣的圖標。 enter image description here

回答

1

首先要從

https://github.com/jpardogo/PagerSlidingTabStrip/blob/master/library/src/com/astuetz/PagerSlidingTabStrip.java

複製整個代碼,使自定義選項卡類。

那麼你應該改變 notifyDataSetChanged()類並實現FOR LOOP中的任何你喜歡的東西! 即可以看到每個角度變得膨脹,並添加到標籤地帶

編輯:

你做這樣的事情

public void notifyDataSetChanged() { 
    tabsContainer.removeAllViews(); 
    tabCount = pager.getAdapter().getCount(); 
    View tabView; 
    for (int i = 0; i < tabCount; i++) { 

     tabView = LayoutInflater.from(getContext()).inflate(YOUR_CUSTOM_LAYOUT, this, false); 
     CharSequence title = pager.getAdapter().getPageTitle(i); 
     addTab(i, title,icons[i], tabView); 
    } 

    updateTabStyles(); 
    getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

     @SuppressWarnings("deprecation") 
     @SuppressLint("NewApi") 
     @Override 
     public void onGlobalLayout() { 

      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
       getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      } else { 
       getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } 
     } 
    }); 
} 


private void addTab(final int position, CharSequence title,int drawableId, View tabView) { 
    TextView textView = (TextView) tabView.findViewById(TEXTVIEW_IN_CUSTOMLAYOUT); 
    ImageView imageView = (ImageView) tabView.findViewById(IMAGEVIEW_IN_CUSTOMLAYOUT); 
    if (textView != null) { 
     if (title != null) textView.setText(title); 
    } 

    if (imageView != null) { 
     if (drawableId != 0) imageView.setIamgeResource(drawableId); 
    } 

    tabView.setFocusable(true); 
    tabView.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (pager.getCurrentItem() != position) { 
       View tab = tabsContainer.getChildAt(pager.getCurrentItem()); 
       notSelected(tab); 
       pager.setCurrentItem(position); 
      } else if (tabReselectedListener != null) { 
       tabReselectedListener.onTabReselected(position); 
      } 
     } 
    }); 

    tabsContainer.addView(tabView, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams); 
} 
+0

請你提供了這樣的代碼片段, –

+0

@KiranPardeshi我加入代碼 –

+0

我應該在Florent的MaterialViewPager的MainActivity中寫這個嗎? –