在片段之间滑动:如何突出显示标签?

时间:2014-08-10 16:42:25

标签: android tabs swipe highlight

我正在编辑朋友应用的源代码,并试图找出如何处理片段。在应用程序中我有一个带有3个选项卡的活动,当我点击其中一个选项卡时,所选选项卡将突出显示。当我在屏幕上滑动时从标签切换到另一个标签时不会发生这种情况。如何编辑代码以增加这种可能性?

public class ViewMyPage extends FragmentActivity{

    List<Fragment> fragments = new Vector<Fragment>();  
    private PagerAdapter mPagerAdapter;
    private ViewPager mPager;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewmerchant);

        Bundle b = new Bundle();
        b.putString("id", getIntent().getExtras().getString("id"));

        fragments.add(Fragment.instantiate(this,SchedaMerchant.class.getName(),b));
        fragments.add(Fragment.instantiate(this,DescrizioneMerchant.class.getName(),b));
        fragments.add(Fragment.instantiate(this,ListaRecensioni.class.getName(),b));

        this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);

        mPager = (ViewPager) super.findViewById(R.id.pager);
        mPager.setAdapter(this.mPagerAdapter);
        final Button button = (Button)findViewById(R.id.buttonscheda);
        final Button button2 = (Button)findViewById(R.id.buttondesc);
        final Button button1 = (Button)findViewById(R.id.buttonrec);

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) { 
                button.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_8));
                button2.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button1.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                mPager.setCurrentItem(0);   // go to first tab
            }
        });

        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                button.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button2.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_8));
                button1.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                mPager.setCurrentItem(1);   // go to second tab
            }
        });

        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                button.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button2.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button1.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_8));
                mPager.setCurrentItem(2);   // go to third tab
            }
        });
    }   
}

public class PagerAdapter扩展了FragmentPagerAdapter {

   // fragments to instantiate in the viewpager
   private List<Fragment> fragments;

   // constructor
   public PagerAdapter(FragmentManager fm,List<Fragment> fragments) {
      super(fm);
      this.fragments = fragments;
   }

   // return access to fragment from position, required override
   @Override
   public Fragment getItem(int position) {
      return this.fragments.get(position);
   }

   // number of fragments in list, required override
   @Override
   public int getCount() {
      return this.fragments.size();
   }
}

1 个答案:

答案 0 :(得分:0)

解决了这个问题:

mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                button.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_8));
                button2.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button1.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
            } else if (position == 1) {
                button.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button2.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_8));
                button1.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
            } else if (position == 2) {
                button.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button2.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_11));
                button1.setBackgroundDrawable(getResources().getDrawable(R.color.transparent_black_hex_8));
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
        }
});

对不起我的帖子......