2014-12-05 68 views
1

目前我使用viewpager做旋轉木馬菜單viewpager更新的TextView(如粗體)

這裏是如何我設置的viewpager

 name = menu.substring(b, menu.length() - 1);//just some strings 
     fragments = getFragments(); 
     pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments); 
     pager = (ViewPager) findViewById(R.id.viewpager); 
     pager.setPageMargin(-700); 
     pager.setOffscreenPageLimit(Menu.size()); 
     pager.setAdapter(pageAdapter); 
     pager.setOnPageChangeListener(pageAdapter); 
     pager.setCurrentItem(pager.getAdapter().getCount() - 1, false); 
     pager.setPageTransformer(true, new ZoomOutPageTransformer()); 

private List<Fragment> getFragments() { 
    fList.add(MyFragment.newInstance(name)); 
    return fList; 
} 

然後我pageadapter類

class MyPageAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener { 
    private List<Fragment> fragments; 
    private FragmentManager fm; 

    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { 
     super(fm); 
     this.fm = fm; 
     this.fragments = fragments; 
    } 
    @Override 
    public Fragment getItem(int position) { 
     return this.fragments.get(position); 
    } 
    @Override 
    public int getCount() { 
     return this.fragments.size(); 
    } 
    @Override 
    public void onPageScrollStateChanged(int state) { 
    } 
    @Override 
    public void onPageSelected(int arg0) { 
    } 
} 

這裏是MyFragment類

 public static final MyFragment newInstance(String message) { 
     MyFragment f = new MyFragment(); 
     bdl = new Bundle(1); 
     bdl.putString(EXTRA_MESSAGE, message); 
     f.setArguments(bdl); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     String message = getArguments().getString(EXTRA_MESSAGE); 
     v = inflater.inflate(R.layout.myfragment_layout2, container, false); 
     TextView messageTextView = (TextView) v.findViewById(R.id.textView); 
     messageTextView.setText(message); 

     return v; 
    } 

我想更新的TextView messageTextView大膽當視圖是在中間,而其他的TextView的視圖的其餘部分取消粗體,像下面

http://imgur.com/9EyZDM1

任何人的圖片可以幫助我如何去做這個?謝謝!

回答

0

您可以通過使用MyPageAdaptergetItem(pos)獲得您的上一個,當前和下一個片段視圖,在獲得視圖後,請檢查這些視圖是否有效。

更換你MyPageAdapter's onPageSelected()如下,

@Override 
     public void onPageSelected(int pos) { 

      if (pos - 1 >= 0) { 
       Fragment prevFragment = getItem(pos - 1); 
       if (prevFragment.isVisible()) { 
        View currentView = prevFragment.getView(); 
        ((TextView) currentView.findViewById(R.id.textView)) 
          .setTypeface(null, Typeface.BOLD); 
       } 
      } 

      if (pos + 1 < fragments.size()) { 
       Fragment nextFragment = getItem(pos); 
       if (nextFragment.isVisible()) { 
        View currentView = nextFragment.getView(); 
        ((TextView) currentView.findViewById(R.id.textView)) 
          .setTypeface(null, Typeface.BOLD); 
       } 
      } 

      Fragment currentFragment = getItem(pos); 
      View currentView = currentFragment.getView(); 
      ((TextView) currentView.findViewById(R.id.textView)).setTypeface(null,Typeface.BOLD); 
     } 
0

如果您查看有關ViewPagerAndroid documentation,可以使用ViewPager.setOnPageChangeListener()查看完整示例。

我認爲這是一個好的開始。從那裏你通知當前Fragment它是「選中」(你的案例中粗體文本)和「取消選擇」前一個。